How to Call CMS Block Using Layout XML in Magento 2

In this article we am going to explain in Magento 2 how to call CMS block using layout XML. In Magento 2, you can call a CMS block using Layout XML by adding a reference block in your layout file.

Let's get started!!

Identify the name or identifier of the CMS block you want to call. Navigate to your theme's layout directory. Typically, this would be located at app/design/frontend/YourVendor/YourTheme. Create or edit the layout XML file where you want to call the CMS block. This could be default.xml or any specific layout file depending on your requirements.

Inside the XML file, add the following code to reference the CMS block:

<referenceBlock name="block_name"> <block class="Magento\Cms\Block\Block" name="block_identifier"> <arguments> <argument name="block_id" xsi:type="string">block_identifier</argument> </arguments> </block> </referenceBlock>

Replace block_name with the name of the block you want to reference. Replace block_identifier with the identifier of the CMS block you want to call. This is the identifier you set when creating the CMS block in the Magento admin panel.

Here's an example of how the XML might look:

<referenceBlock name="sidebar.additional"> <block class="Magento\Cms\Block\Block" name="your_cms_block"> <arguments> <argument name="block_id" xsi:type="string">your_cms_block_identifier</argument> </arguments> </block> </referenceBlock>

Save the changes to your layout XML file. And, open Command line in folder root of magento and run both command to clen the cache.

php bin/magento c:c

Bingo!! Now, the CMS block with the specified identifier should be called and displayed in the specified block area according to your layout XML configuration.

I hope this article helped you to find what you were looking for.

Bookmark it for your future reference. Do comment below if you have any other questions on that.

P.S. Do share this article with your team.

Review other articles maybe it'll help you too.

Recent Articles
Chrome Extension
Copyright © 2024 All rights reserved.
Ads OFF toggle_off