Friday, April 21, 2017

Magento: How to check if current page is homepage?

Here is a quick Magento code to check if the 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 'You are in Homepage!';
} else {
    echo 'You are NOT in Homepage!';
}

If you are elsewhere (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 'You are in Homepage!';
} else {
    echo 'You are NOT in Homepage!';
}

Below 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 'You are in Homepage!';
} else {
    echo 'You are NOT in Homepage!';
}

Hope this helps. Thanks

No comments:

Post a Comment