How To Call CMS Static Block In Magento2
No Worries if you don’t know how to Call CMS Static Block in Magento2, we’ll tell you the easiest way to do this,
Before continuing to this initially, we’ll briefly tell you about Static Blocks in case you are unfamiliar:
Static Blocks: or we can say CMS blocks, are those chunks of content which can be placed anywhere in the page. Generally they can be used to display static information.
They’re perfect for banners, size charts, return policies, and anything that would make your CONTENT easier and manageable.
Let’s have a look on how to create static block:
Jump on to your admin Panel
Under Content proceed to Block
Then Click on Add New Block
Update your new block details and then save
Yes! you made your new static block in a couple of minutes.
Let’s release how to call!
There are several methods through which you can call CMS static blocks, well it depends on how you need it to function:
1.XML: Calling a static block from xml is suitable for content that will remain in a consistent position in your theme.
Let’s see how
Embed this below code snippet in your xml file:
<referenceContainer name="content"> <block class="Magento\Cms\Block\Block" name="block_name"> <arguments> <argument name="block_id" xsi:type="string">your_block_identifier</argument> </arguments> </block> </referenceContainer>
Place your block identifier that you have added for your static block in the admin panel and also I had shown above in case you won’t understand. Don’t forget to flush the cache.
Let’s release the second one!
2.Phtml : Adding your static block inline with PHP is the quickest way to get your block in your site.
Let’s see how
Embed this below code snippet in your phtml file
echo $this->getLayout() ->createBlock('Magento\Cms\Block\Block') ->setBlockId('your_block_identifier') ->toHtml();
Place your block identifier right after the setBlockId.
That’s it, don’t forget to flush the cache.
Let’s proceed to the last but not least i.e.
3.CMS Content: This method is used when you want to display the CMS static block in CMS content.
Let’s see how!
Embed this short line of code in your block
{{block class="Magento\\Cms\\Block\\Block" block_id="your_block_identifier"}}
This code will place the block “your_block_identifier” inline in your CMS page.
- Conclusion: The whole idea of creating these static blocks is to smoothen the amount of time it takes to update your site.
Now, you don’t have to dive into that jumbled template files to find where you put that couple paragraphs of content.
We hope this blog helps you a lot.In case of any query, let me know in the comment section.