Tuesday, May 14, 2013

How to test if you're on the homepage of Joomla 2.5 and Joomla! 3.x series

To determine if the user was viewing the front page of Joomla! 2.5.x and Joomla! 3.x use code like this:

if you don't use multi-lingual feature of Joomla! the same languageuse this code:
<?php
$app = JFactory::getApplication();
$menu = $app->getMenu();
if ($menu->getActive() == $menu->getDefault()) {
echo 'This is the front page';
}
?>

If multi-lingual feature used the front page is dependent on the currently selected language, you must code like this to check frontpage:
<?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';
}
?>

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';
}
?>
Disqus Comments