Magento 2 offers great flexibility for developers when it comes to displaying content dynamically. One powerful feature is the ability to call static block in Magento 2 PHTML files, which allows you to manage and display predefined content efficiently across your stores.
In this guide, we'll walk you through the process of calling a static block in .phtml file in Magento 2
Create or edit the XML file in your custom theme following the path:-
Step-by-Step Guide:
Step 1: Get the Block Identifier
The first step is to identify the static block you want to display. You can find the block identifier by navigating to Content > Blocks in the Magento Admin panel.
Step 2: Define the Static Block in the XML Layout
To effectively call static block in PHTML Magento 2, you need to reference the Magento 2 static block in the layout XML file. This is essential depending on where you want to display the block (product page, category page, etc.) Example: Call Static Block in customer/account/login/ pagecustomer_account_login.xml

customer_account_login.xml
app/design/frontend/Vendor_Name/Theme_Name/Magento_Customer/layout/
Here's the XML code you need to add:
<?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <body> <referenceBlock name="customer_form_login"> <block class="Magento\Cms\Block\Block" name="discount-coupon-code"> <arguments> <argument name="block_id" xsi:type="string">discount-coupon-code</argument> </arguments> </block> </referenceBlock> </body> </page>Replace discount-coupon-code with the identifier of your static block.
Step 3: Render the Static Block in a .phtml File
To render the block in a .phtml file, you can use getChildHtml() to call the block defined in the XML layout. This is another crucial step when you want to call a static block in PHTML Magento 2. Create or edit the PHTML file in your custom theme following path:-app/design/frontend/Vendor_Name/Theme_Name/Magento_Customer/templates/form/
Add the following code to render the block:
<?php echo $block->getChildHtml('discount-coupon-code'); ?>

Step 4: Clear the Magento cache
Afetr making there changes, you need to refresh the cache for them to take effect. Run this command in your terminalphp bin/magento cache:flush