Magento 2 static blocks are helpful to admin to add and control HTML code to be displayed in the frontend. Generally, admin uses the static block for size charts, offer banners, sale promotions, return policies, etc. The entire objective of creating these static blocks is to streamline the amount of time it takes to update your site. You don’t need to dive into the hundreds of lines of code to update the changes every time!
Using Phtml file is one of the ways to call CMS static block in Magento 2. I’ll show here how to call CMS block in phtml file in Magento 2. Using the below method, you can simplify your tasks such as managing that offer bar in the store or the sale banner that you’ve put up for the festive season!
Before calling CMS block in phtml file in Magento 2, first of all, you need to create a static block:
Now we can call the created static block in phtml file as below.
Call your static block in phtml file:
1 2 3 4 5 6 | <?php echo $this->getLayout() ->createBlock('Magento\Cms\Block\Block') ->setBlockId('your_block_identifier') ->toHtml(); ?> |
Implement the above code which will save a lot of your time that can be focussed for the betterment of the business!
In the case of any doubts regarding the topic, please use the comments section below. I’d be happy to help you out 😊
Rate the post with 5 stars if found useful.
Happy Coding 😃