Static blocks in Magento 2 allow store owners to create reusable content, such as banners, promotional messages, and custom HTML, and place them in various locations across their site. For developers, knowing how to call a static block in XML is essential for better content management and improved store customization. This guide shows you the steps to quickly add a static block to specific layout areas using XML in Magento 2.
What is a Static Block?
In Magento 2, a static block is a pre-configured block of content managed via the admin panel. Once created, it can be called directly in XML layout files, making it ideal for placing custom content on specific pages or sections of your store.
Calling a Static Block in XML
To display a static block in Magento 2 using XML, follow these steps.
1) Identify the Block’s Container
Choose the area where you want to display the block, such as the main content area, header, or footer.
2) Use the XML Code
<referenceContainer name="content">
<block class="Magento\Cms\Block\Block" name="custom_static_block">
<arguments>
<argument name="block_id" xsi:type="string">your_block_identifier</argument>
</arguments>
</block>
</referenceContainer>
Replace "content"
with the desired container (header
, footer
, etc.), and set "your_block_identifier"
to match the unique identifier of the static block created in the admin.
3) Clear Cache
After updating the XML file, clear the Magento 2 cache to see the changes reflected on the front end.
Example: Adding a Static Block to the order success page
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="order.success.additional.info">
<block class="Magento\Cms\Block\Block" name="checkout-success-additional-info">
<arguments>
<argument name="block_id" xsi:type="string">checkout-success-additional-info</argument>
</arguments>
</block>
</referenceContainer>
</body>
</page>
By using static blocks, you can easily manage promotional banners, announcements, and custom HTML without modifying your theme files every time. This approach saves time and makes content updates more flexible, especially for non-developers. Whether you’re customizing the footer, header, or main content area, following these steps will give you more control over the layout and appearance of your Magento 2 site.
Feel free to explore other ways to enhance your Magento 2 store by combining static blocks with dynamic content strategies.
Happy customizing!
P.S. Do share this note with your team.
You may also like:
Magento 2: How to use helper in PHTML?
Leave a Comment