Bobcares

Magento 2 CMS block in PHTML | Guide

by | Sep 9, 2023

Learn how to set up Magento 2 CMS block in PHTML. Our Magento Support team is here to help you with your questions and concerns.

Magento 2 CMS block in PHTML | Guide

Did you know we can include CMS blocks in PHTML in Magento 2?

This lets us display dynamic content or custom content blocks in different parts of our e-commerce website.

Magento 2 CMS block in PHTML

In other words, we get to combine Magento’s CMS capabilities with PHP templates to create dynamic and customized pages.

Let’s take a quick look at how to get this done:

  1. First, we have to create a CMS block in the Magento admin panel. So, head to Block under Elements in the Content Section.
  2. Then, click Add New Block button and enter the required information.
  3. Next, we have to assign the block to a certain specific page/pages. So, head to the CMS page where we want to include the block.
  4. Then, go to the Content section of the CMS page editor, and reference the CMS block as seen here:

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

    Remember to replace “block_identifier” with our CMS block’s identifier.

  5. Now we have to insert it into a PHTML template file. So, open the PHTML file where we want to include the CMS block. Then, use the following code:

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

  6. Furthermore, we can apply custom CSS styles and additional HTML markup to the CMS block within the PHTML template to match our website’s design and layout.
  7. Now, we have to clear the Magento cache so that the updated content is displayed correctly on the frontend.

After the above steps, we can dynamically include and display Magento 2 CMS blocks within PHTML templates.

[Need assistance with a different issue? Our team is available 24/7.]

Conclusion

In brief, our Support Experts demonstrated how to set up the CMS block in PHTML.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.