In Magento sometimes we need to find out if current page is homepage or not, for some condition. So in this tutorial I will show you how to check if current page is homepage or not.
If you are in template/page/html/header.phtml template file, then you can check for homepage with the following code:
if($this->getIsHomePage()) { echo 'This is Homepage.'; } else { echo 'NOT a Homepage.'; }
If you are in any other .phtml template file or in any other .php class file then you can use the following code:
if(Mage::getBlockSingleton('page/html_header')->getIsHomePage()) { echo 'This is Homepage.'; } else { echo 'NOT a Homepage.'; }
Following is an alternative way to check for homepage:
$routeName = Mage::app()->getRequest()->getRouteName(); $identifier = Mage::getSingleton('cms/page')->getIdentifier(); if($routeName == 'cms' && $identifier == 'home') { echo 'This is Homepage.'; } else { echo 'NOT a Homepage.'; }
Hope this will help you.