How to Call CMS Static Block in Phtml File in Magento 2
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:
- Login to Magento 2, move to Content –> Blocks. Click “Add New Block“, add below information and save configuration.
- Enable Block: Enable the block.
- Block Title: Name of the block to easily identify the purpose of creating the block.
- Identifier: Enter a unique block identifier.
- Store View: Select the store views to show the static block in.
- Block Content: Add content of the block.
- Once the static block is saved, it can be seen enlisted in the grid.
Now we can call the created static block in phtml file as below.
Method to call CMS static block in phtml file in Magento 2
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(); ?> |
Display CMS Static Block In CMS Content:
1 |
{{block class="Magento\\Cms\\Block\\Block" block_id="block_identifier"}} |
Display CMS Static Block In XML:
Code to show the CMS Static Block in the “Content”
1 2 3 4 5 6 7 |
<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> |
Implement the above code which will save a lot of your time that can be focussed for the betterment of the business! You can also simply use the Page Builder to call block in Magento 2.
You may have a look at the programmatic method to call helper function in pHTML in Magento 2.
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 ?
Still need help? Hire our Adobe-certified Magento experts.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
8 Comments
Where do we place his code though? I used to know how to do I in Magento 1, but can not find the Custom design options in Magento 2
Hello Phil,
Copy this code and paste it in phtml file and change the name of static block.
Thank you.
How can I get the title of Cms static block in Magento 2 ?
Hello,
Please refer “How To Get CMS Block Data By Identifier In Magento 2” for the solution.
Thanks.
how to call static content into Block php file?
Hi Ramalakshmi,
to call static content into the block PHP file, put the below function in your block file:
public function getcmsData()
{
return $this->cmsblock->setBlockId(‘block_identifier’)->toHtml();
}
Thanks good supportive block for magneto . I am call one controller(this controller extend another controller ) to another module block but error come getLayout() are not define in controller please help us
The Layout must be defined to call the block else it will not work.