How to Call CMS Block Using Layout XML in Magento 2
In this article we are 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:
saveCopyzoom_out_map<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:
saveCopyzoom_out_map<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.
saveCopyzoom_out_mapphp 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.
- Magento 2 Create Category Attribute Programmatically
- How to Add Image Uploader on Store Configuration in Magento 2
- How to Add Custom Select/MultiSelect Options on Store Configuration in Magento 2
- How to Add Dynamic Row on Store Configuration in Magento 2
- How to Add Date & time Picker on Store Configuration in Magento 2
- How to Add Link on Store Configuration Fields Comment in Magento 2
- How to Use Javascript Mixins in Magento 2
- How to Add Custom Validation Rule in Magento 2
- How to Get Product Collection
- How to Setup Magento 2 PWA Studio
- How to Get Current Category
- How to Get Current Product
- Get Product Collection with Filters
- How to Create And Use Plugin In Magento 2