Check if homepage with PHP in Magento

This snippet wraps around your homepage-specific code in order to only display when on the homepage of your store.

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.

Useful post? Share it

15 comments on “Check if homepage with PHP in Magento

      1. 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.

        1. &lt;?php if ($this-&gt;getIsHomePage()):?&gt;
          //Homepage-only code
          &lt;?php else:?&gt;
          //Code for all the other pages in Magento
          &lt;?php endif?&gt;

          There it is. This one also worked for me. 😀

          1. 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.

  1. Hi Phil, I noticed you have a “love this” heart button in the upper right of your blog.

    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?

    1. 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.

      1. 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. 😀

  2. 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-&gt;getUrl('') == $this-&gt;getUrl('*/*/*', array('_current'=&gt;true, '_use_rewrite'=&gt;true));

    You can replace the getUrl() method with Mage::getUrl():

    $_isHomePage = Mage::getUrl() == Mage::getUrl('*/*/*', array('_current'=&gt;true, '_use_rewrite'=&gt;true));

  3. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *