Magento - How to find if you are on homepage - getIsHomePage

Posted by Damodar Bashyal on July 31, 2012

 

Today I needed to exclude hot products and recently viewed products block from homepage, so I needed to find if the current page is homepage. The first block of code worked on header.phtml template but not in main page template, so I ended up using 2nd option.

If you need to check this on header.phtml template then use this code:

<?php
if($this->getIsHomePage()) {
    echo 'You are in Homepage!';
} else {
    echo 'You are NOT in Homepage!';
}
?>

If you want to check from other pages than do this:

<?php
$is_homepage = Mage::getBlockSingleton('page/html_header')->getIsHomePage();
if($is_homepage)
{
	echo "You are on Homepage!";
} else {
	echo "You are NOT on Homepage!";
}
?>

or,

<?php
$routeName = Mage::app()->getRequest()->getRouteName();
$identifier = Mage::getSingleton('cms/page')->getIdentifier();
 
if($routeName == 'cms' && $identifier == 'home') {
    echo 'You are in Homepage!';
} else {
    echo 'You are NOT in Homepage!';
}
?>

GoWebBaby Magento Development posted on - Wednesday 3rd of October 2012 11:23:40 AM

Thanks for this code snippet. Really useful for me.

Kushu posted on - Friday 4th of January 2013 09:02:13 AM

Thanks that helped me to check if I am on the homepage as I am new to Magento commerce software. Also loved your all Magento Tips and Tricks articles. It would be good if you could add subscription form based on post categories.

Thangadurai posted on - Wednesday 11th of December 2013 07:40:57 AM

Thanks for sharing the code snippet. It is very useful.

tim posted on - Tuesday 11th of November 2014 01:38:13 AM

All these scripts tell me that i'm still not on my homepage when i know i am... any idea why?

kirbeenanette posted on - Thursday 8th of December 2016 02:56:22 AM

Thanks for sharing,,,,,,, nice!!!
 
not published on website


QR Code: Magento - How to find if you are on homepage - getIsHomePage