How can you check if you are on the main page in Joomla?

I am working on a Joomla website and I need the front page to look a little different from the rest of the pages, but not enough to guarantee the use of two themes (itโ€™s a pain to update two stylesheets and two sets of images every time I want to make a small change )

My thoughts are to do a little test in the index.php of the template: if we are on the main page, serve X, otherwise we will serve Y. However, I'm not quite sure how to check this. I can't just use the url because url.com/ and url.com/index.php and url.com/index.php? etc. etc. are valid.

Does anyone know how to do what I'm trying to do? Like the variable $ _JOOMLA ['page'] or something like this?

Thanks! --Mala

+7
php joomla
source share
10 answers
if(JRequest::getVar('view') == "frontpage" ) { //You are in! } else { //You are out! } 
+8
source share

To be sure that the client is on the main page, you should check: "The current page (Itemid) is selected as the default menu item" as this code (for Joomla 1.6, 1.7 and 2.5):

 <?php $menu = JFactory::getApplication()->getMenu(); if ($menu->getActive() == $menu->getDefault()) { echo 'This is the front page'; } ?> 

To find the code for Joomla 1.5, see http://docs.joomla.org/How_to_determine_if_the_user_is_viewing_the_front_page

+7
source share

This works for me, I had problems with any other way.

 $app = JFactory::getApplication(); if ($app->getMenu()->getActive()->home) { $homepage=true; } 
+5
source share

For Joomla 2.5 and 3.x use the following code for a site with one language:

 <?php $app = JFactory::getApplication(); $menu = $app->getMenu(); if ($menu->getActive() == $menu->getDefault()) { echo 'This is homepage'; } ?> 

For multilingual sites, the definition of the main page (main page) depends on the currently selected language, so you will need to use something like the following:

 <?php $app = JFactory::getApplication(); $menu = $app->getMenu(); if ($menu->getActive() == $menu->getDefault( 'en-GB' )) { echo 'This is English homepage'; } elseif ($menu->getActive() == $menu->getDefault( 'it-IT' )) { echo 'This is Italian homepage'; } ?> 

For multilingual sites, you can also use the following code:

 <?php $app = JFactory::getApplication(); $menu = $app->getMenu(); $lang = JFactory::getLanguage(); if ($menu->getActive() == $menu->getDefault($lang->getTag())) { echo 'This is homepage'; } else { echo 'This is not homepage'; } ?> 

Hope this helps!

+3
source share

for Joomla 1.6 and 1.7 it will be as follows:

 if(JRequest::getVar('view') == "featured" ) { //You are in! } else { //You are out! } 
+2
source share

For Joomla.6, nothing worked for me

+1
source share

also you can define each page:

 <?php $active = JFactory::getApplication()->getMenu()->getActive(); ?> <body class="<?php echo $active->alias; ?> "> 
+1
source share

use this

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

In Joomla 3.x, to display some content only on the main page. you can use

 <?php $menu = JSite::getMenu(); if ($menu->getActive() == $menu->getDefault()) : ?> Some code here to show only on front page <?php endif ?> 

And to show something everywhere except the frontpage, just deny! =

 <?php $menu = JSite::getMenu(); if ($menu->getActive() != $menu->getDefault()) : ?> Some code here to show everywhere except frontpage <?php endif ?> 
0
source share

As RB already pointed out, itโ€™s also wise to check the menu language, just in case there are several โ€œhome pagesโ€, as they are on multilingual sites.

 <?php // Determine if we are on the homepage $lang = JFactory::getLanguage(); $langTag = $lang ? JFactory::getLanguage()->getTag() : null; $isHomepage = $langTag ? ($menu->getActive() == $menu->getDefault($langTag)) : ($menu->getActive() == $menu->getDefault()); ?> 

Then, where you need website-only content:

 <?php if ($isHomepage) : ?> <div class="homepage-markup"> </div> <?php endif; ?> 
0
source share

All Articles