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

From Joomla! Documentation

(Amended code for 1.6, 1.7 and multi-lingual sites. Thanks JM.)
Line 46: Line 46:
 
}
 
}
 
?>
 
?>
 +
</source>
 +
 +
For multi-lingual sites, it could also be necessary to display a specific code/html for '''all''' Default Home pages.
 +
<source lang="php">
 +
<?php $menu = JSite::getMenu(); ?>
 +
<?php $lang = JFactory::getLanguage(); ?>
 +
<?php if ($menu->getActive() == $menu->getDefault($lang->getTag())) : ?>
 +
etc.
 
</source>
 
</source>
  

Revision as of 02:06, 8 October 2011

Joomla 1.0 Joomla 1.0[edit]

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 Joomla 1.5[edit]

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 Joomla 1.7 Joomla 1.6 and 1.7[edit]

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

For multi-lingual sites, it could also be necessary to display a specific code/html for all Default Home pages.

<?php $menu = JSite::getMenu(); ?>
<?php $lang = JFactory::getLanguage(); ?>
<?php if ($menu->getActive() == $menu->getDefault($lang->getTag())) : ?>
etc.