How to Call CMS Block Page in PHTML File in Magento 2

In this block, I am showing you how to call the cms block page inside Magento 2.

Whenever we customize the theme or create a new website, at that time we often need to show the content of the block page inside our p file.

Often we call CMS static block to display better data on the frontend.

Many times through CMS block we add slider image, banner image, or sometimes some special product, sales promotions, return policies, etc., and to call it in many places, we call that block in phtml file.

I am telling you step by step method how you can show data by calling the block page in a PHTML file.

Call CMS Block Page in PHTML

Before You Call Block Page in Magento 2 website you must have that block page.

To create a new block page, you can create a new blog by going inside the admin content.

Then you can decide its identifier yourself if you want and you can use that identifier wherever you want to call it.

 

Create Static Block Page in Magento 2

Login to Magento 2

Admin > Content > Block > Add New Block

Enable Block: When you enable the block, then the content is visible; if you disable it, the content is disabled

Block Title: The title of the block decides what purpose it is made for and what it should be known as.

Identifier: The identifier is basically to call that block. There can never be two blocks of a bean identifier. The identifier is always unique.

Store View: Select the store views to show the static block in.

Block Content: Whatever content you want to add inside the block like Image, Text, Title, Heading, Table, Product, etc. then these are all content when you call block anywhere then this content will appear

When you save the block, it appears as a content grid, then if you want to edit it in HTML format, then you can edit anything in it by clicking on the so as HTML.

Magento 2 CMS Block Page

 

Read more: Add Multiple Product Attribute Using InstallData in Magento2

 

How to Call CMS Block Page in PHTML File in Magento 2

Now below are the steps to call the CMS static block page in phtml file.

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

or

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

 

Call Block Identifier in CMS Page

By using this below method you can display CMS static block content in any cms block page or any static block page.

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

 

How to Get CMS Static Block In XML

Code to display the CMS Static Block in the “Content.

If you want to call static block inside XML file then you can follow the below method for that.

In the XML file, you can mention either the identifier or the ID of the block and through that, you can call the block.

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

You can call the CMS static block by adopting any of these methods.

Wherever you want to show data, whether it is a slider, whether it is a product, whether it is a promotion or any kind of block content, you can easily call it in any phtml file and show it on the frontend.

If you face any kind of problem in this then you can feel free to call us, talk or mail, we will be there 24*7 to assist you.

 

Similar Posts:

Add Custom Class on Checkout Form Field in Magento 2

Magento 2.4 Installation Guide: How to Install Magento 2.4.2