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

From Joomla! Documentation

 
(15 intermediate revisions by 5 users not shown)
Line 1: Line 1:
== Joomla 1.0 ==
+
<noinclude><languages /></noinclude>
 +
{{version|1.0,1.5,2.5,3.x}}== {{JVer|1.0}} Joomla! 1.0 ==
 +
 
 +
<translate><!--T:1-->
 +
In Joomla! 1.0.x it was possible to determine if the user was viewing the front page by using code like this:</translate>
  
In Joomla! 1.0.x it was possible to determine if the user was viewing the front page by using code like this:
 
 
<source lang="php">
 
<source lang="php">
 
<?php
 
<?php
Line 10: Line 13:
 
</source>
 
</source>
  
== Joomla 1.5 ==
+
== {{JVer|1.5}} Joomla! 1.5 ==
 +
 
 +
<translate><!--T:2-->
 +
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</translate>
  
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
 
 
<source lang="php">
 
<source lang="php">
 
<?php
 
<?php
Line 21: Line 26:
 
?>
 
?>
 
</source>
 
</source>
This works by checking to see if the current active menu item is the default one.
 
  
== Joomla 1.6 ==
+
<translate><!--T:3-->
 +
This works by checking to see if the current active menu item is the default one.</translate>
 +
 
 +
<translate>== {{JVer/multi|2.5,3.x}} Joomla! 2.5 and 3.x series == <!--T:4--></translate>
 +
 
 +
<translate><!--T: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:</translate>
 +
 
 +
<source lang="php">
 +
<?php
 +
$app = JFactory::getApplication();
 +
$menu = $app->getMenu();
 +
if ($menu->getActive() == $menu->getDefault()) {
 +
echo 'This is the front page';
 +
}
 +
?>
 +
</source>
 +
 
 +
<translate><!--T:6-->
 +
For multi-lingual sites the front page is dependent on the currently selected language, so you will need to use code like this:</translate>
  
For the 1.6 version, the conditional statement, became even shorter. This is the result for Joomla! 1.6.x
 
 
<source lang="php">
 
<source lang="php">
 
<?php
 
<?php
if(JRequest::getVar(‘view’) == “featured” ) :
+
$app = JFactory::getApplication();
    echo 'This is the front page';
+
$menu = $app->getMenu();
else :
+
if ($menu->getActive() == $menu->getDefault( 'en-GB' )) {
    echo 'This is the other pages';
+
echo 'This is the front page';
endif;
+
}
 +
elseif ($menu->getActive() == $menu->getDefault( 'fr-FR' )) {
 +
echo 'Accueil';
 +
}
 
?>
 
?>
 
</source>
 
</source>
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.
+
 
 +
<translate><!--T:7-->
 +
For multi-lingual sites, it could also be necessary to display a specific code/html for '''all''' Default Home pages.</translate>
 +
 
 
<source lang="php">
 
<source lang="php">
 
<?php
 
<?php
$menu = &JSite::getMenu();
+
$app = JFactory::getApplication();
if (JRequest::getInt(‘Itemid’) == $menu->getDefault()) : ?>
+
$menu = $app->getMenu();
    echo 'This is the front page';
+
$lang = JFactory::getLanguage();
else :
+
if ($menu->getActive() == $menu->getDefault($lang->getTag())) {
    echo 'This is the other pages';
+
echo 'This is the front page';
endif;
+
}
 +
else {
 +
echo 'Accueil';
 +
}
 
?>
 
?>
 
</source>
 
</source>
 +
 
<noinclude>
 
<noinclude>
 +
<translate>
 +
<!--T:8-->
 
[[Category:Development]]
 
[[Category:Development]]
 
[[Category:Tutorials]]
 
[[Category:Tutorials]]
 +
</translate>
 
</noinclude>
 
</noinclude>

Latest revision as of 04:42, 23 July 2015

Other languages:
English • ‎Nederlands • ‎español • ‎français

Joomla 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:

<?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 2.5 Joomla 3.x Joomla! 2.5 and 3.x series[edit]

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