2013-03-07 26 views
2

如何僅在joomla的主頁上顯示消息? 我有興趣在site.com上展示它,而不是在site.com/index.php/page或其他任何網站上展示它。如何僅在joomla主頁上顯示消息?

我已經測試了以下內容:

<?php $app = JFactory::getApplication(); 
    $menu = $app->getMenu(); 
    $lang = JFactory::getLanguage(); 
if ($menu->getActive() == $menu->getDefault($lang->getTag())) : ?>this is the homepage 
    <?php endif; ?> 

<?php $menu = & JSite::getMenu(); 
if ($menu->getActive() == $menu->getDefault()) { 
    echo "this is the homepage"; 
} 
?> 

的問題是,我仍然可以看到消息「這是主頁」像http://site.com/index.php/category/id/78-article頁,明確ISN」在主頁上。似乎只要鏈接中有index.php,上面的代碼就認爲它屬於主頁。

+0

什麼是您使用的joomla版本? – HamZa 2013-03-07 21:19:42

+0

Joomla 3.0 !!!!! – 10now 2013-03-07 21:20:42

+2

你可以更好的瞭解如何檢查主頁http://docs.joomla.org/How_to_determine_if_the_user_is_viewing_the_front_page – 2013-03-07 21:24:33

回答

1

OK,只是一個想法,但嘗試:

if (preg_match('/index\.php$/',$_SERVER['PHP_SELF'])) { 
    echo "this is the homepage"; 
} 
2

這有什麼好做的鏈接 '的index.php'。相反,它與http://site.com/index.php/category/id/78-article的鏈接沒有與其關聯的菜單項有關。做的正是你想要什麼,你可能會需要得到的代碼需要一些技巧和檢查,以確保實際的頁面信息的網頁信息一致:

$jinput = JFactory::getApplication()->input; 
$menu = & JSite::getMenu(); 
$active = $menu->getActive(); 
$default = $menu->getDefault(); 
if (
    $active == $default && 
    $jinput->get('option') == $default->query['option'] && 
    $jinput->get('view') == $default->query['view'] && 
    $jinput->get('id') == $default->query['id'] 
) { 
    echo "This is the homepage"; 
} 

我檢查的默認菜單項選項(哪個組件)以及視圖和ID值與輸入中設置的值進行比較。

http://site.com/index.php/category/id/78-article此鏈接會將ID設置爲78,並且很可能會改變視圖和選項,從主頁菜單中定義的視圖和選項開始,因此不會觸發。

+0

不錯的+1,只是在開頭的錯字'$ jinput' – HamZa 2013-03-07 22:17:19

+1

謝謝!修正了打字錯誤,意味着它們都是$ jinput,因爲這是它通常在文檔中的方式。 – 2013-03-07 23:42:48

0

你應該試試這個。 1.從管理員部分檢查已分配了「主頁」(這是主頁)的菜單。複製那裏顯示的鏈接。 它會像

index.php?option=com_somecomponent&view=someview 
  • 現在轉到您的模板的index.php。
  • 寫狀態有

    if(JRequest::getVar('option')=='com_something' && JRequest::getVar('view')=='someview'){ //show message } 
    
  • 這應該爲你做的伎倆!但請記住。如果你改變主頁的菜單,這將不得不相應地改變。

    相關問題