3 Easy Methods to Call CMS Static Block in Magento 2

magento2-cms-static-block

CMS blocks are vital assets of Magento 2. Using CMS blocks, you can display text, images, videos, and other static content anywhere on Magento 2 site. Not only static content but you can also add dynamic content using widgets in static blocks.

You do not need to write any code to add the contents. Magento 2 CMS blocks provide WYSIWYG Editor which is very easy to use. Using this editor you can style your text and insert the images easily. In case you have already created a static block, you might be looking for how to call cms static block in other Pages and files.

In this post we will see how to call CMS static block in CMS page, layout XML file and PHTML file.

Call CMS Static Block in CMS Page

If you are creating a static page and want to call some blocks on that page, you can use the below code on your page. You have to replace your block identifier in the block_id parameter.

{{block class="Magento\Cms\Block\Block" block_id="block_identifier"}}

Call CMS Static Block in Layout XML File

Sometimes while developing custom module, you need to insert CMS block inside your XML layout file. To call CMS static block in layout XML file you can use below code. You have to replace your block identifier in block_id parameter.

<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>

Call CMS Static Block in PHTML File

If above two methods are not feasible and you need to call CMS block in PHTML file, you can use below code. Replace setBlockId argument with your block identifier.

<?= $this->getLayout()
->createBlock('Magento\Cms\Block\Block')
->setBlockId('block_identifier')
->toHtml();

Conclusion

As per your requirement, you can call a static block in CMS pages, XML layout files, and PHTML files. Got question? Feel free to ask in the comment box below.

Want to ask a question or leave a comment?

Leave a Comment

(0 Comments)

All the comments are goes into moderation before approval. Irrelevant comment with links directly goes to spam. Your email address will not be published.

Was this post helpful? Please support Us!

Follow us on twitter or Like us on facebook.

 


Subscribe Here
SUBSCRIBE NOW

Want to Increase your Magento 2 Development Knowledge?

If Yes! Then Subscribe to our newsletter and receive step-by-step tutorials, code snippets once a week!!
close-link

To Avoid Spam Downloads, We Want Your Email

We will send you download link right
away. Please submit form below.
SEND ME DOWNLOAD LINK
Close