How to Call CMS Static Block in Phtml File in Magento 2

Magento 2 (Adobe Commerce) static blocks are helpful for the admin to add and control HTML code to be displayed in the frontend. Generally, admins use 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 a Phtml file is one of the ways to call a CMS static block in Magento 2. I’ll show here how to call the CMS block in a phtml file in Magento 2. Using the method below, you can simplify your tasks, such as managing the offer bar in the store or the sale banner that you’ve put up for the festive season!

Before calling the CMS block in the phtml file in Magento 2 (Adobe Commerce), first of all, you need to create a static block:

1. Log in to Magento 2, move to Content –> Blocks. Click “Add New Block“, add the information below, and save the configuration.

  1. Enable Block: Enable the block.
  2. Block Title: Name of the block to easily identify the purpose of creating the block.
  3. Identifier: Enter a unique block identifier.
  4. Store View: Select the store views to show the static block in.
  5. Block Content: Add content of the block.

2. Once the static block is saved, it can be seen listed in the grid.

static-block

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 the phtml file:

<?php
echo $this->getLayout()
->createBlock('Magento\Cms\Block\Block')
->setBlockId('your_block_identifier')
->toHtml();
?>

Display CMS Static Block In CMS Content:

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

Code to show the CMS Static Block in the “Content”

<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 focused on 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.

Happy Coding?

Sanjay Jethva

Article by

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