15

I am trying to add a static block to the home page of a Magento site using the layout XML file.

I can see how to add and remove block inside a reference, but I am struggling to see how to add it for a specific page, i.e. the home page.

<block type="cms/block" name="home-page-block">
    <action method="setBlockId"><block_id>home-page-block</block_id></action>
</block>

How would I wrap this code in the page.xml file for it to be only used on the homepage?
Or is there a better way? Should the home page be a new template?

Mat
  • 202,337
  • 40
  • 393
  • 406
Alan Whitelaw
  • 16,171
  • 9
  • 34
  • 51

1 Answers1

29

In any layout file used by your theme add the following.

<cms_index_index>
  <reference name="content">
    <block type="cms/block" name="home-page-block">
      <action method="setBlockId"><block_id>home-page-block</block_id></action>
    </block>
  </reference>
</cms_index_index>

cms_index_index is specific to the home page.

bogatyrjov
  • 5,317
  • 9
  • 37
  • 61
clockworkgeek
  • 37,650
  • 9
  • 89
  • 127
  • 1
    Thanks, I have got it to work by adding a reference tag around the block and giving the reference a name attribute of content. Does this mean that there has to be a call in the .phtml template to a "reference" and anything with that name is rendered? – Alan Whitelaw Dec 06 '10 at 13:03
  • 1
    I forgot about the reference part! Somewhere in each page .phtml template there will be a `getChildHtml('content')` in place. That is why it works. – clockworkgeek Dec 06 '10 at 13:06
  • Thanks again, that answers my original question, however I don't seem to be able to add getChildHtml('home_flash') in the .phtml file and use in the XML file. Any ideas why? – Alan Whitelaw Dec 06 '10 at 13:56
  • Because `reference` only refers to an existing block, it does not create a new one. In the above example you already created a block called `home-page-block` and could explicitly show it with `getChildHtml('home-page-block') ?>` – clockworkgeek Dec 06 '10 at 14:15
  • @snh_nl That is complex enough to be it's own question, so please start a new one. – clockworkgeek Aug 27 '14 at 15:43