Reputation: 348
I'm creating a magento 2 theme. I want to display the custom blocks on the cms homepage.
I want to know to show the static blocks in phtml and xml layout, cms page contents
How can I do?
Upvotes: 11
Views: 21291
Reputation: 11
none of these work for putting static blocks via default.xml Please remit proper examples. In 2.0.7 When I apply to my theme the following after creating a static block in administration named socialimg / socialimg as block identifier nothing shows up. nor even in memory. not even a search of exception.log or system.log
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="header.panel">
<block class="Magento\Framework\View\Element\Html\Links" name="header.links">
<arguments>
<argument name="css_class" xsi:type="string">header links</argument>
</arguments>
</block>
</referenceContainer>
<referenceBlock name="socialimg1">
<block class="Magento\Cms\Block\Block" name="socialimg">
<arguments>
<argument name="block_id" xsi:type="string">socialimg</argument>
</arguments>
</block>
</referenceBlock>
<referenceBlock name="logo">
<arguments>
<argument name="logo_img_width" xsi:type="number">148</argument>
<argument name="logo_img_height" xsi:type="number">43</argument>
</arguments>
</referenceBlock>
<referenceContainer name="footer">
<block class="Magento\Store\Block\Switcher" name="store_switcher" as="store_switcher" after="footer_links" template="switch/stores.phtml"/>
</referenceContainer>
<referenceBlock name="report.bugs" remove="true"/>
<move element="copyright" destination="before.body.end"/>
</body>
</page>
Upvotes: 1
Reputation: 688
Use the following code to display the static block in CMS page -
{{block class="Magento\\Cms\\Block\\Block" block_id="block_identifier"}}
Use the following code to display the static block in phtml file -
<?php echo $block->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('block_identifier')->toHtml();?>
Upvotes: 0
Reputation: 11
The above is correct, additionally, the xml file for the homepage is cms_index_index.xml
inside of the Magento_Cms
folder (and this should go in your own package/theme folder, not in Magento/blank
or app/code
).
Just put the above xml code inside the <body>
tag in that file, depending on the cache settings you may need to clear them to see the adjustments.
Upvotes: 1
Reputation: 722
Please try to use below codes.
In Phtml File:
<?php echo $this->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('block_identifier')->toHtml();?>
in CMS Content:
{{block class="Magento\\Cms\\Block\\Block" block_id="block_identifier"}}
In Xml File:
<referenceContainer name="content">
<block class="Magento\Cms\Block\Block" name="block_identifier">
<arguments>
<argument name="block_id" xsi:type="string">block_identifier</argument>
</arguments>
</block>
</referenceContainer>
Upvotes: 37