Actions

Difference between revisions of "How to determine if the user is viewing the front page"

From Joomla! Documentation

m (Ampersand is superfluous as Joomla 1.6 requires PHP 5.)
m (Removed closing PHP tag which was in the wrong place, fixed incorrect quotes and slightly amended grammar.)
Line 28: Line 28:
 
<source lang="php">
 
<source lang="php">
 
<?php
 
<?php
if(JRequest::getVar(‘view’) == “featured” ) :  
+
if(JRequest::getVar('view') == 'featured' ) :  
 
     echo 'This is the front page';
 
     echo 'This is the front page';
 
else :
 
else :
     echo 'This is the other pages';
+
     echo 'This is NOT the front page';
 
endif;  
 
endif;  
 
?>
 
?>
Line 39: Line 39:
 
<?php
 
<?php
 
$menu = JSite::getMenu();
 
$menu = JSite::getMenu();
if (JRequest::getInt(‘Itemid’) == $menu->getDefault()) : ?>
+
if (JRequest::getInt('Itemid') == $menu->getDefault()) :
 
     echo 'This is the front page';
 
     echo 'This is the front page';
 
else :  
 
else :  
     echo 'This is the other pages';
+
     echo 'This is NOT the front page';
 
endif;  
 
endif;  
 
?>
 
?>

Revision as of 16:19, 1 April 2011

Joomla 1.0

In Joomla! 1.0.x it was possible to determine if the user was viewing the front page by using code like this:

<?php
if ($option == 'com_frontpage' || $option == '') {
        echo 'This is the front page';
}
?>

Joomla 1.5

But in Joomla! 1.5.x the com_frontpage component is no longer present. This is how to achieve the same result in Joomla! 1.5.x

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

This works by checking to see if the current active menu item is the default one.

Joomla 1.6

For the 1.6 version, the conditional statement, became even shorter. This is the result for Joomla! 1.6.x

<?php
if(JRequest::getVar('view') == 'featured' ) : 
    echo 'This is the front page';
else :
    echo 'This is NOT the front page';
endif; 
?>

You can also use the default menu option to check the home page. If the menu is activated, the default item for menu would be home page.

<?php
$menu = JSite::getMenu();
if (JRequest::getInt('Itemid') == $menu->getDefault()) :
    echo 'This is the front page';
else : 
    echo 'This is NOT the front page';
endif; 
?>