This snippet wraps around your homepage-specific code in order to only display when on the homepage of your store.
<?php // IF WE ARE ON HOMEPAGE if( Mage::getSingleton('cms/page')->getIdentifier() == 'home' && Mage::app()->getFrontController()->getRequest()->getRouteName() == 'cms' ) : ?> // Put your homepage-only code in here. <?php endif; ?>
The ‘home’ identifier may be different for your site so amend as necessary.
15 comments on “Check if homepage with PHP in Magento”
Thanks for this really useful snippet. 😀
Glad I could help.
Hi Phil, I would like to share a shorter method I found and this will work for any type of page as long as it’s set in the homepage. 🙂
// Put your homepage-only code in here.
// Put your code in here that will appear for the rest of the pages.
<?php if ($this->getIsHomePage()):?>
//Code for all the other pages in Magento
There it is. This one also worked for me. 😀
Mage_Page_Block_Html_Header::getIsHomePage() doesn’t return true when you are on the root of your site, only when you are on the full url, usualy /cms/index/index.
Oh geez.. I wasn’t able to paste the PHP code properly. How can I comment with php codes in your blog?
Wrap PHP inside [.php] and [/.php] (remove the .’s)
Thanks. But the weird thing is that the was changed to something else. Haha 😀
Hi Phil, I noticed you have a “love this” heart button in the upper right of your blog. http://i.imgur.com/GQIq2sQ.png
How was it saved? Is it cookie based? browser based? IP based? If I click I love this post, and I transfer to another computer, will it still appear?
I really want something like that in my blog. Is it a custom code or a wordpress plugin?
This is stored against the post / page in the database as a counter so remains on the page regardless of browser, IP etc, and yes, it’s custom code.
But if I use another computer, I can place another like right? So it will count as 2 likes if I click it again?
Do you have a tutorial to make that kind of heart+like+counter thing? 😀 It’s really an awesome thing if you can make it a wordpress plugin for it. 😀
Please note that the getIsHomePage()-method is a method of the Mage_Page_Block_Html_Header class, which translates to:
* Check if current url is url for home page
* @return true
public function getIsHomePage()
return $this->getUrl('') == $this->getUrl('*/*/*', array('_current'=>true, '_use_rewrite'=>true));
You can replace the getUrl() method with Mage::getUrl():
$_isHomePage = Mage::getUrl() == Mage::getUrl('*/*/*', array('_current'=>true, '_use_rewrite'=>true));
Thanks! Worked for me, only thing was that in my custom theme the home identifier was not ‘home’, but ‘template_home’, but that was easy enough to figur out.
A little hint for those wanting to figure out what the home identifier is, simply echo the command:
And use the output in the code supplied by Phil.
Thanks Phil, helped me a lot!