Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show the static blocks in Magento 2?

Tags:

php

magento2

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?

like image 203
John Avatar asked Sep 17 '15 01:09

John


2 Answers

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>
like image 132
skynetch Avatar answered Sep 28 '22 14:09

skynetch


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.

like image 37
Klaus De Buysser Avatar answered Sep 28 '22 13:09

Klaus De Buysser