Magento 2 How to call static block in phtml file

Are you wondering how to show CMS Static Block from template phtml file in Magento 2?

Use the below steps if you want to show CMS Static Block in any template in Magento 2:

Create CMS Static Block on Admin Side:

Step 1: Login to your Magento 2 Admin Panel.

Step 2: Go to Content > Blocks from Left Side navigation section. You will see a list of Magento CMS static blocks.

magento2-content-blocks

Step 3: Click on Add New Block button. This will open New Block page.

magento2-addnewblock

Step 4: In the New Block page, enter the following details and click the Save Block button to create the CMS static block.

  • Enable Block: Enable/Disable the block.
  • Block Title: Name of the block. ( i.e, Home Page Block).
  • Identifier: Block identifier. It must be unique. ( i.e, home-page-block ).
  • Store View: Select the store view from the multi-select box.
  • Content: Content of the block.

After completing the above steps, we can display the CMS static blocks in frontend as follows:

Display CMS Static Block In Phtml File:

Here the code to show CMS Static Block in any template (phtml) file in Magento 2,

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

Here the code to show CMS Static Block in any other CMS static block/page in Magento 2,

Display CMS Static Block In CMS Content:

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

Display CMS Static Block In XML:

Here the code to show CMS Static Block in any layout (XML) file in Magento 2,

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

Hope this blog helpful.