How to determine if the user is viewing the front page
From Joomla! Documentation
(Difference between revisions)
Infograf768 (Talk | contribs) (→{{JVer|1.6}} {{JVer|1.7}} Joomla 1.6 and 1.7) |
m (version template for 4 versions of Joomla) |
||
| (5 intermediate revisions by 3 users not shown) | |||
| Line 1: | Line 1: | ||
| − | == {{JVer|1.0}} Joomla 1.0 == | + | {{version|1.0,1.5,1.6,1.7,2.5}}== {{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 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. | ||
| − | == {{JVer|1.6 | + | == {{JVer/multi|1.6,1.7,2.5}} Joomla 1.6, 1.7 and 2.5 == |
| − | + | There are some differences in 1.6/1.7/2.5 to avoid Strict Standards errors. Use the following code for a site where all content is in the same language: | |
<source lang="php"> | <source lang="php"> | ||
<?php | <?php | ||
| − | $ | + | $app = JFactory::getApplication(); |
| + | $menu = $app->getMenu(); | ||
if ($menu->getActive() == $menu->getDefault()) { | if ($menu->getActive() == $menu->getDefault()) { | ||
echo 'This is the front page'; | echo 'This is the front page'; | ||
| Line 38: | Line 39: | ||
<source lang="php"> | <source lang="php"> | ||
<?php | <?php | ||
| − | $ | + | $app = JFactory::getApplication(); |
| + | $menu = $app->getMenu(); | ||
if ($menu->getActive() == $menu->getDefault( 'en-GB' )) { | if ($menu->getActive() == $menu->getDefault( 'en-GB' )) { | ||
echo 'This is the front page'; | echo 'This is the front page'; | ||
| Line 50: | Line 52: | ||
For multi-lingual sites, it could also be necessary to display a specific code/html for '''all''' Default Home pages. | For multi-lingual sites, it could also be necessary to display a specific code/html for '''all''' Default Home pages. | ||
<source lang="php"> | <source lang="php"> | ||
| − | <?php $ | + | <?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'; | ||
| + | } | ||
| + | ?> | ||
</source> | </source> | ||
| − | |||
<noinclude> | <noinclude> | ||
Revision as of 14:23, 13 September 2012
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, 1.7 and 2.5
There are some differences in 1.6/1.7/2.5 to avoid Strict Standards errors. Use the following code for a site where all content is in the same language:
<?php $app = JFactory::getApplication(); $menu = $app->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 $app = JFactory::getApplication(); $menu = $app->getMenu(); if ($menu->getActive() == $menu->getDefault( 'en-GB' )) { echo 'This is the front page'; } elseif ($menu->getActive() == $menu->getDefault( 'fr-FR' )) { echo 'Accueil'; } ?>
For multi-lingual sites, it could also be necessary to display a specific code/html for all Default Home pages.
<?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'; } ?>