Page 1 of 1

home page check

Posted: Wed Feb 25, 2009 2:27 pm
by igd
in my template index.php what basic php if/else can i do to check and see if I am on the home page.

Re: home page check

Posted: Wed Feb 25, 2009 3:11 pm
by ooffick
Hi,

the simplest one would be to do the following:

Code: Select all

<?php if(JRequest::getVar('view') == "frontpage" ) : ?>
<!-- your home page code -->
<?php else : ?>
<!-- your code -->
<?php endif; ?>
Olaf

Re: home page check

Posted: Wed Feb 25, 2009 9:22 pm
by igd
cheers your awesome.

Re: home page check

Posted: Wed Jun 16, 2010 10:43 pm
by andpatton
I recently wanted to do a home page check in a template for a site that didn't use the 'frontpage' view, which made Olaf's method inapplicable.

There's a fairly easy way to do a more robust check, however, one that checks what the default menu item is and if it's the current menu item, rather than just checking if it's the frontpage view:

Code: Select all

<?php $menu = &JSite::getMenu();
if (JRequest::getInt('Itemid') == $menu->getDefault()) : ?>
<!-- your home page code -->
<?php else : ?>
<!-- your code -->
<?php endif; ?>

Re: home page check

Posted: Sun Jan 09, 2011 6:26 pm
by safani
I have been searching the forums and have yet to find an answer to my particular issue. I need to have a php if and else statement that will output certain javascript if joomla is on a certain page only. The page is not the home page, so I can't use that code. I have looked for ways to filter to whatever page you want, but to no avail.

If page is 2nd menu link, then print these javascripts in head, else do not print javascipts.

Re: home page check

Posted: Mon Jan 10, 2011 3:34 pm
by andpatton
safani wrote:If page is 2nd menu link, then print these javascripts in head, else do not print javascipts.
You only want this JS on a single page? If so, just find out that menu item's ID (go to Menus -> [Your Menu], and find the number in the "ItemID" column for the page's menu item). Then, put the following code in the <head> of your template, where you replace your_id with the number you just found:

Code: Select all

<?php if (JRequest::getInt('Itemid') == your_id) : ?>
<!-- special javascript -->
<?php endif; ?>
The "special javascript" will only be printed if your are currently viewing the page specified.

Re: home page check

Posted: Fri Jan 28, 2011 1:17 pm
by osnysantos

Code: Select all

<?php if (JRequest::getInt('Itemid') == your_id) : ?>
<!-- special javascript -->
<?php endif; ?>
This is the only solution that works in Joomla 1.6 apparently...
Does anyone know another solution for Joomla 1.6?

Re: home page check

Posted: Fri Jan 28, 2011 4:14 pm
by andpatton
osnysantos wrote:Does anyone know another solution for Joomla 1.6?
You just need to adapt one line of the existing code, I believe. I don’t have an install where I quickly test this, but

Code: Select all

<?php $menu = JFactory::getApplication()->getMenu();
if (JRequest::getInt('Itemid') == $menu->getDefault()) : ?>
<!-- your home page code -->
<?php else : ?>
<!-- your code -->
<?php endif; ?>
should work. The method of getting the $menu is the only thing that has changed, as far as I can tell.

Re: home page check

Posted: Mon Jan 31, 2011 12:47 am
by osnysantos
doesn't work...

Re: home page check

Posted: Thu Mar 24, 2011 8:50 am
by kurian_86
@andpatton:

I am using joomla 1.6 and I have used your suggested code but its else portion is working just. It is not getting the "if" portion. Can anybody guide me please?

Regards and thanks

Re: home page check

Posted: Wed Apr 20, 2011 4:43 pm
by teensicle
what if i want to check for pages other than the homepage?

Re: home page check

Posted: Thu Apr 21, 2011 7:05 am
by kurian_86

Re: home page check

Posted: Fri Sep 23, 2011 7:03 pm
by ServatorD
I am working in Joomla 1.7 and this worked perfectly for me.

Re: home page check

Posted: Mon Feb 13, 2012 1:47 am
by mdb172
Yes, 1.7 solution works in 2.5, also.

Code: Select all

<?php
$menu = JSite::getMenu();
if ($menu->getActive() == $menu->getDefault()) {
	echo 'This is the front page';
}
?>
from here:
http://docs.joomla.org/How_to_determine ... front_page

Re: home page check

Posted: Thu May 31, 2012 11:18 pm
by bbolli
Being Joomla is OOP you can chain the methods preventing any need to declare classes.

Code: Select all

<?php if (JFactory::getApplication()->getMenu()->getActive() == JFactory::getApplication()->getMenu()->getDefault()) : ?>
   echo "I'm a front page!";
<?php else : ?>
   echo "I bring dishonor to this page...";
<?php endif; ?>
If someone needs multi-language or varying default pages depending on language you should certainly review the Joomla Documentation link from mdb172.

Re: home page check

Posted: Fri Jun 01, 2012 3:46 pm
by bbolli
You should just assume that what works in 1.5 won't work in 2.5. Since 1.6 and 1.7 were transitions, a lot of legacy functionality was depreciated, meaning some legacy extensions still worked. That is not the case with 2.5 which officially removed all depreciated classes and methods.

The below code does the same for 1.5:

Code: Select all

if (JSite::getMenu()->getActive() == JSite::getMenu()->getDefault()) {
	echo 'This is the front page';
} 

Re: home page check

Posted: Fri Jul 06, 2012 12:44 pm
by admod
Hi pals,
in Joomla 2.5.6

Code: Select all

<?php if(JRequest::getVar('view') == "featured" ) : ?>
<!-- your home page code -->
<?php else : ?>
<!-- your code -->
<?php endif; ?>
will work, pls check that in other versions too

Thanks
Anes

Re: home page check

Posted: Sat Jan 19, 2013 4:26 am
by natalierey
This works for joomla but it doesnt work in Jomres inner pages. I am using jomres on home page and when I click on a property to view property details it still renders it on the home page so

Code: Select all

if($this->countModules( 'position_name' ) 
doesnt work on those inner pages as it will still be consideres home page. you have to implement custom checks by getting current URL etc.

Code: Select all

$app = JFactory::getApplication();
    $menu = $app->getMenu();
    if ($menu->getActive() == $menu->getDefault() && !$matched) {

Re: home page check

Posted: Thu Apr 18, 2013 1:29 am
by garkell
In Joomla! 3.0.3 I did the following which works on basic test, but if anyone can point out any issues with using this mechanism I would appreciate it.

Code: Select all

$app = JFactory::getApplication();
$menu = $app->getMenu();
if ($menu->getActive()->home) {
    echo 'This is Home Page'; 
}

Re: home page check

Posted: Fri Jun 07, 2013 6:39 pm
by nikonor

Code: Select all

if(JURI::root()!=JURI::current()) {
//not mainpage
}else{
//mainpage
}