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?
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>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With