11/01/2010

How to Show static block only on homepage in magento

If you are in header.phtml file then you can call $this->getIsHomepage();

But if you are not there then you must have to create instance of Mage_Page_Block_Html_Header(); then write the following code to check homepage

<?php
$ref = new Mage_Page_Block_Html_Header();
if($ref->getIsHomePage()){ ?>
<div class="homepage"><?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('Your_StaticBlock_Id')->toHtml();?></div>
<?php
}
?>

8 comments:

  1. Can you tell me how the oppossite works. So to show a block every where on the store accept on the homepage.

    ReplyDelete
    Replies
    1. you can do by if(!$ref->getIsHomePage()){}

      Delete
  2. Hi,
    It is working fine, but i have three languages and when i change language from home page this static not showing there.
    working on: http://127.0.0.1/magento-1.7.0.2-fashiono/magento/index.php
    not working on: http://127.0.0.1/magento-1.7.0.2-fashiono/magento/index.php/?___store=french&___from_store=default

    can you please help.

    Thanks, paddy

    ReplyDelete
  3. $ref->getIsHomePage() is not working

    ReplyDelete
  4. Used it for banner slider on homepage. Thanks

    ReplyDelete