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.
Using the 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. 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!
it will be displayed on the frontend by using any of the following methods,
- Display the CMS static block in the phtml file.
- Display the CMS static block in the CMS content.
- Di1splay 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 the Left Side navigation section. You will see a list of Magento CMS static blocks.
Step 3: Click on Add New Block button. This will open New Block page.
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:
1 2 3 4 5 6 |
<?php echo $block->getLayout() ->createBlock('Magento\Cms\Block\Block') ->setBlockId('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:
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> |
In the case of any doubts regarding the topic, please use the comments section below.