15

In magento 1.x it is possible to add a static block using xml layout file.

<reference name="root">
        <block type="cms/block" name="name">
            <action method="setBlockId"><block_id>static-block-id</block_id></action>
        </block>
</reference>

But, in magento 2 how we can achieve it ?

4 Answers 4

25

Finally I got the solution. You can call static block using following way.

<referenceContainer name="footer">
    <block class="Magento\Store\Block\Switcher" name="store_switcher" as="store_switcher" after="footer_links" template="switch/stores.phtml"/>

     <block class="Magento\Cms\Block\Block" name="test">
        <arguments>
            <argument name="block_id" xsi:type="string">promo</argument>
        </arguments>
    </block>
</referenceContainer>
2
  • How did you find out the reference name?
    – tread
    Dec 20, 2016 at 9:56
  • @StevieG Either locate it or read the default XML implementations so you know how everything is built up (very useful to do for long term).
    – user4351
    Apr 7, 2018 at 12:01
8

It can be done easily from the admin panel by following the steps below.

  1. Go to Content -> Widgets
  2. Add New Widget
  3. Select type as CMS Static Block
  4. Select Design Package/Theme And Continue
  5. Set Widget Title, Store and Sort order in Storefront properties tab
  6. Select static block from in the Widget options tab
  7. Save the widget
  8. Click On Add Layout Update button
  9. Select Option from Display on dropdown e.g. : Specific page to display block on specifi

  10. Select Page from the Page dropdown

  11. Select Container from Container dropdown
  12. save the widget
3
  • It is useful to CMS pages only. Can we use it in our custom module xml too? Dec 9, 2015 at 13:31
  • I have provided the cms page example only, you can use it for every possible module or page. you can get an idea by seeing the list of the option in the Display on dropdown Dec 9, 2015 at 13:46
  • This is excellent, clean and simple. Thank you.
    – dawhoo
    Apr 14, 2017 at 15:55
6

If you want to call static-block in 2columns-left page, you can simply do it using,

here below you can change quickblock with your static-block identifier.

<referenceContainer name="sidebar.main">
            <block class="Magento\Cms\Block\Block" name="quick-block">
                <arguments>
                    <argument name="block_id" xsi:type="string">quickblock</argument>
                </arguments>
            </block>
</referenceContainer>

Above Block is working fine for 2columns-left with sidebar.

2
  • Hi If we want to call the static block only for category page layout then? i did call static block in xml with sidebar.additional but it's not working. What could be the reason?
    – jack
    Nov 15, 2017 at 6:06
  • @Rakesh, <?php echo $this->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('identiefier_id')->toHtml(); ?> I used in success.phtml , its not working Nov 29, 2018 at 11:08
2

In the following code, change the "block_identifier" from the CMS block created on magento dashboard and add it to layout.xml. It works.

<referenceContainer name="footer">
       <block class="Magento\Cms\Block\Block" name="block_identifier">
            <arguments>
                <argument name="block_id" xsi:type="string">block_identifier</argument>
            </arguments>
        </block>
    </referenceContainer>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.