Magento2 | PWA | GraphQL

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:

1. Login to Magento 2, move to Content –> Blocks. Click “Add New Block“, add below information and save 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 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:

<?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"}}

Display CMS Static Block In XML:

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 focussed for the betterment of the business!

Tag : Magento2
0 Comments On "How to Call CMS Static Block in Phtml File in Magento 2"

Back To Top