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

Magento 2 has the in-built feature called CMS static blocks which allows the store owners to create/control the static content on the admin side. The static content may be the promotion banner, return policies, size charts, etc. After creating the CMS blocks on the admin side, it will be displayed on the frontend by using any of the following methods,

  1. Display the CMS static block in the phtml File.
  2. Display the CMS static block in the CMS content.
  3. Display the CMS static block in the XML file.

Create CMS Static Block on Admin Side:

Before calling the CMS block in the frontend, first, we need to create it. Here the steps to create new CMS static block on the Magento 2 admin panel,

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.

Magento 2: How to Call CMS Static Block in Phtml File - 1

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

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

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 the frontend by using the methods below,

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();
?>

Display CMS Static Block In CMS Content:

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

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

Read: Magento 2: Creating CMS Static Block Using Installable Script

Hope this helps.

Leave a Reply

Your email address will not be published. Required fields are marked *