The question:
How i include a static block in a page using xml. For example i created a static block with identifier promo. In magento 1 we include a static block using below code
<block type="cms/block" name="Promo">
<action method="setBlockId"><block_id>promo</block_id></action>
</block>
I want to do exactly same in magento 2
The Solutions:
Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.
Method 1
<block class="MagentoCmsBlockBlock" name="Promo">
<arguments>
<argument name="block_id" xsi:type="string">promo</argument>
</arguments>
</block>
This is equivalent to M1.
Method 2
Try below code.
Call from Phtml File:
<?php echo $block->getLayout()->createBlock('MagentoCmsBlockBlock')->setBlockId('block_identifier')->toHtml();?>
Call from cms page or block:
{{block class="Magento\Cms\Block\Block" block_id="block_identifier"}}
Call from Xml File:
<referenceContainer name="content">
<block class="MagentoCmsBlockBlock" name="block_identifier">
<arguments>
<argument name="block_id" xsi:type="string">block_identifier</argument>
</arguments>
</block>
</referenceContainer>
Hope this helps you!
Method 3
Call from Xml File worked.
Change the “block_identifier” from the CMS block created on magento dashboard and add it to layout.xml
<referenceContainer name="footer">
<block class="MagentoCmsBlockBlock" name="block_identifier">
<arguments>
<argument name="block_id" xsi:type="string">block_identifier</argument>
</arguments>
</block>
</referenceContainer>
Method 4
Try below code:
<block class="MagentoCmsBlockBlock" name="news">
<arguments>
<argument name="block_id" xsi:type="string">news</argument>
</arguments>
</block>
It works for me!Hope this helps!
Method 5
Above image you can see my block identifier= “product_view_right_sidebar”. add below code to your xml file
<referenceContainer name="content">
<block class="MagentoCmsBlockBlock" name="product_view_right_sidebar">
<arguments>
<argument name="block_id" xsi:type="string">product_view_right_sidebar</argument>
</arguments>
</block>
</referenceContainer>
Method 6
<referenceContainer name="content">
<block class="MagentoCmsBlockBlock" name="daily_deals" template="bigbazaar.phtml">
<arguments>
<argument name="block_id" xsi:type="string">daily_deals</argument>
</arguments>
</block>
</referenceContainer>
Here daily_deals
is my CMS block identifier’s name. I wish it should work..
Method 7
Since MagentoCmsBlockBlock
class is deprecated, you should use:
<block name="custom_block_name" class="MagentoCmsBlockBlockByIdentifier">
<arguments>
<argument name="identifier" xsi:type="string">custom_block_identifier</argument>
</arguments>
</block>
Method 8
You can call CMS block on Magento2 in following 6 ways:-
- Call static block on category page in Magento2
- Display CMS block with Magento 2 widgets
- Insert CMS block into Magento 2 WYSIWYG editor
- Call the CMS block using layout XML
- Add static block from template phtml file in Magento 2
- Show Static Block within another block in Magento2
For calling CMS block on layout, we can use following code –
Considering your block identifier is – promo.
<block class="MagentoCmsBlockBlock" name="Promo">
<arguments>
<argument name="block_id" xsi:type="string">promo</argument>
</arguments>
</block>
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0