How to determine if the user is viewing the front page
From Joomla! Documentation
(Difference between revisions)
m (Removed closing PHP tag which was in the wrong place, fixed incorrect quotes and slightly amended grammar.) |
(Amended code for 1.6, 1.7 and multi-lingual sites. Thanks JM.) |
||
| Line 1: | Line 1: | ||
| − | == Joomla 1.0 == | + | == {{JVer|1.0}} 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: | In Joomla! 1.0.x it was possible to determine if the user was viewing the front page by using code like this: | ||
| Line 10: | Line 10: | ||
</source> | </source> | ||
| − | == Joomla 1.5 == | + | == {{JVer|1.5}} 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 | 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 | ||
| Line 23: | Line 23: | ||
This works by checking to see if the current active menu item is the default one. | This works by checking to see if the current active menu item is the default one. | ||
| − | == Joomla 1.6 == | + | == {{JVer|1.6}} {{JVer|1.7}} Joomla 1.6 and 1.7 == |
| − | + | The same code that worked in Joomla 1.5 will work in 1.6 onwards. However, since PHP 5 is now a minimum requirement for Joomla 1.6 the ampersand (&) is no longer required on the first line. Use the following code for a site where all content is in the same language: | |
<source lang="php"> | <source lang="php"> | ||
<?php | <?php | ||
| − | + | $menu = JSite::getMenu(); | |
| − | + | if ($menu->getActive() == $menu->getDefault()) { | |
| − | + | echo 'This is the front page'; | |
| − | + | } | |
| − | + | ||
?> | ?> | ||
</source> | </source> | ||
| − | + | ||
| + | For multi-lingual sites the front page is dependent on the currently selected language, so you will need to use code like this: | ||
<source lang="php"> | <source lang="php"> | ||
<?php | <?php | ||
$menu = JSite::getMenu(); | $menu = JSite::getMenu(); | ||
| − | if ( | + | if ($menu->getActive() == $menu->getDefault( 'en-GB' )) { |
| − | + | echo 'This is the front page'; | |
| − | + | } | |
| − | + | elseif ($menu->getActive() == $menu->getDefault( 'fr-FR' )) { | |
| − | + | echo 'Accueil'; | |
| + | } | ||
?> | ?> | ||
</source> | </source> | ||
| + | |||
| + | |||
<noinclude> | <noinclude> | ||
[[Category:Development]] | [[Category:Development]] | ||
[[Category:Tutorials]] | [[Category:Tutorials]] | ||
</noinclude> | </noinclude> | ||
Revision as of 05:13, 18 June 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 and 1.7
The same code that worked in Joomla 1.5 will work in 1.6 onwards. However, since PHP 5 is now a minimum requirement for Joomla 1.6 the ampersand (&) is no longer required on the first line. Use the following code for a site where all content is in the same language:
<?php $menu = JSite::getMenu(); if ($menu->getActive() == $menu->getDefault()) { echo 'This is the front page'; } ?>
For multi-lingual sites the front page is dependent on the currently selected language, so you will need to use code like this:
<?php $menu = JSite::getMenu(); if ($menu->getActive() == $menu->getDefault( 'en-GB' )) { echo 'This is the front page'; } elseif ($menu->getActive() == $menu->getDefault( 'fr-FR' )) { echo 'Accueil'; } ?>