2009-11-18 36 views
7

我正在Joomla網站上工作,我需要首頁看起來與其餘頁面略有不同,但不足以保證使用兩個主題(很難更新兩個樣式表和兩個樣式表每次我想做一個小小的改變時都會有一組圖像)。如何測試您是否在Joomla的主頁上?

我的想法是在模板的index.php中進行一點測試:如果我們在主頁上,則服務X,否則服務Y.但是,我並不完全知道如何測試這個。我不能只使用網址,因爲url.com/和url.com/index.php和url.com/index.php?等等等都是有效的。

有沒有人知道一種方法來做我想做的事情?像$ _JOOMLA ['page']變量或類似的方便嗎?

謝謝! --Mala

回答

8
if(JRequest::getVar('view') == "frontpage") { 
    //You are in! 
} 
else { 
    //You are out! 
} 
+0

謝謝!這正是我所期待的。 – Mala 2009-11-18 20:35:24

+2

如果安裝具有名爲「frontpage」的視圖的其他組件,則可能會中斷。爲了確保你在查看內容組件,我會這樣寫'if'語句:if(JRequest :: getVar('view')=='frontpage'&& JRequest :: getVar('option')= ='com_content') – jlleblanc 2009-11-18 21:52:38

+1

如果您在首頁使用com_content正面頁面視圖,而不是其他組件,則它可以正常工作。 – 2009-11-19 07:31:36

1

爲Joomla。6,無非是this別人爲我工作:

2

爲Joomla 1.6和1.7這將是如下:

if(JRequest::getVar('view') == "featured") { 
    //You are in! 
} 
else { 
    //You are out! 
} 
7

要舒爾該客戶端在主頁上,您應該測試「當前頁面(Itemid)選擇爲默認菜單項」像這樣的代碼做的(對於Joomla 1.6,1.7和2.5):

<?php 
$menu = JFactory::getApplication()->getMenu(); 
if ($menu->getActive() == $menu->getDefault()) { 
    echo 'This is the front page'; 
} 
?> 

要查找代碼爲Joomla 1.5,看http://docs.joomla.org/How_to_determine_if_the_user_is_viewing_the_front_page

+0

爲3.0這不起作用 – themis 2014-07-15 11:09:55

5

這對我的作品,我不得不使用其他任何方式

$app = JFactory::getApplication(); 
if ($app->getMenu()->getActive()->home) { 
    $homepage=true; 
} 
+0

我很難過。我已經建立了自己的小模板。沒什麼。但要檢查主頁,只有你的方法工作(不是由Soufiane Hassou的那個。你知道爲什麼嗎?謝謝! – itsols 2013-04-13 04:20:42

+0

我相信Soufiane Hassou的方式適用於joomla 1.5,我的測試是在2.5 ..我的方式問題wont在Joomla 1.5中工作我不得不測試它看到我提供的方式也適用於多種語言Joomla 1.5不是以同樣的方式製作多種語言的版本 – StiGMaT 2013-04-30 14:07:42

+0

適用於joomla 3這適用於插件 – themis 2014-07-15 11:09:18

0

使用這一個

<?php 
$app = JFactory::getApplication(); 
$menu = $app->getMenu(); 
$lang = JFactory::getLanguage(); 
if ($menu->getActive() == $menu->getDefault($lang->getTag())) { 
     echo 'This is the front page'; 
} 
else { 
     echo 'Accueil'; 
} 
?> 
1

還可以定義麻煩每頁:

<?php 
$active = JFactory::getApplication()->getMenu()->getActive(); 
?> 
<body class="<?php echo $active->alias; ?> "> 
3

爲Joomla 2.5和3.x使用下面的代碼用單一語言站點:

<?php 
$app = JFactory::getApplication(); 
$menu = $app->getMenu(); 
if ($menu->getActive() == $menu->getDefault()) { 
    echo 'This is homepage'; 
} 
?> 

對於多語言網站,檢測網頁(頭版)取決於當前所選的語言,所以你需要使用類似以下內容:

<?php 
$app = JFactory::getApplication(); 
$menu = $app->getMenu(); 
if ($menu->getActive() == $menu->getDefault('en-GB')) { 
    echo 'This is English homepage'; 
} 
elseif ($menu->getActive() == $menu->getDefault('it-IT')) { 
    echo 'This is Italian homepage'; 
} 
?> 

對於多語言網站,您還可以使用下面的代碼:

<?php 
$app = JFactory::getApplication(); 
$menu = $app->getMenu(); 
$lang = JFactory::getLanguage(); 
if ($menu->getActive() == $menu->getDefault($lang->getTag())) { 
    echo 'This is homepage'; 
} 
else { 
    echo 'This is not homepage'; 
} 
?> 

希望它能幫助!

+0

完美地工作,也很好的解釋。謝謝。 – jackJoe 2018-02-19 10:14:31

0

在的Joomla 3.x中表現出一定的內容只在FrontPage中可以使用

<?php $menu = JSite::getMenu(); 
    if ($menu->getActive() == $menu->getDefault()) : ?> 
Some code here to show only on front page 
<?php endif ?> 

,並顯示出除了頭版處處東西只是否定!=

<?php $menu = JSite::getMenu(); 
    if ($menu->getActive() != $menu->getDefault()) : ?> 
    Some code here to show everywhere except frontpage 
<?php endif ?> 
0

由於R.B.已經指出,檢查菜單項的語言也是合理的,以防萬一在多語言網站中存在多個「主頁」。

<?php // Determine if we are on the homepage 
$lang = JFactory::getLanguage(); 
$langTag = $lang ? JFactory::getLanguage()->getTag() : null; 

$isHomepage = $langTag ? ($menu->getActive() == $menu->getDefault($langTag)) : ($menu->getActive() == $menu->getDefault()); ?> 

然後,你只想主頁內容:

<?php if ($isHomepage) : ?> 
    <div class="homepage-markup"> 

    </div> 
<?php endif; ?> 
相關問題