How to check the current page is homepage or not in Magento?
April 21, 2012
In the magento all the pages define by "Identifier". First we will check the current page is a CMS page or not after that we will check the current page identifier is "home" or not. We will check for "home" identifier because by default magento home page identifier be "home"
To check the current page is CMS page or not, we cans use the below code:
$page = Mage::app()->getFrontController()->getRequest()->getRouteName();
if ($page == 'cms'):
echo "current page is cms page";
Now we will check the cms page identifier using below code:
$result=(Mage::getSingleton('cms/page')->getIdentifier()=='home') ? true:false;
If we will merge both code which are above:
$result=false;
$page = Mage::app()->getFrontController()->getRequest()->getRouteName();
if ($page == 'cms'):
$isnothome=(Mage::getSingleton('cms/page')->getIdentifier()=='home') ? false :true;
endif;
<?php if($isnothome) : ?>
echo "We are in homepage";
<?php else: ?>
echo "It is not a homepage";
<?php endif; ?>
Enjoy :)