Magento : Check if Current Page is Homepage?

There are two ways to check check if Current Page as Homepage in Magento.

1) Global way to get check either in class file or template file :

<?php

if(Mage::getBlockSingleton(‘page/html_header’)->getIsHomePage()) {
echo ‘It is a Homepage!’;
} else {
echo ‘It is NOT a Homepage!’;
}

?>

 

OR

<?php

$routeName = Mage::app()->getRequest()->getRouteName();
$identifier = Mage::getSingleton(‘cms/page’)->getIdentifier();

if($routeName == ‘cms’ && $identifier == ‘home’) {
echo ‘It is a Homepage!’;
} else {
echo ‘It is NOT a Homepage!’;
}

?>

 

2) If you are in template/page/html/header.phtml template file, following code snippet is useful to check for homepage:

<?php

if($this->getIsHomePage()) {
echo ‘It is a Homepage!’;
} else {
echo ‘It is NOT a Homepage!’;
}

?>

 

Leave a Reply