How To

Magento 2: Call Custom phtml file on all CMS Pages

Hello Magento Friends,

Welcome back to our “How To” Magento 2 technical blog series.

With Magento 2 sometimes we need to add dynamic content to our CMS pages. Calling a custom phtml file is a good move. So let us learn about Magento 2: Call Custom phtml file on all CMS Pages.

Steps to Call Custom phtml file on all CMS Pages:

Step 1: First, you need to create cms_page_view.xml in the following path

app\code\Vendor\Extension\view\frontend\layout\cms_page_view.xml

<?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>
        <referenceContainer name="content">
         <block class="Magento\Framework\View\Element\Template" name="custom_phtml_in_cms" template="Vendor_Extension::call_allcms.phtml" />
        </referenceContainer>
    </body>
</page>

Step 2: Now you need to add the call_allcms.phtml  file in the following path

app\code\Vendor\Extension\view\frontend\templates\call_allcms.phtml

<?php 
 echo "Call this phtml file in all cms pages";
?>

That’s it!

Conclusion:

Following the above steps, you are able to call custom phtml file on all CMS pages of Magento 2. For any doubts or queries, let me know in the comment section and I will be happy to help you. Help your Magento friends by forwarding the article to them. Hit 5 stars if you found the article helpful and stay connected with us!

Happy Coding!

Click to rate this post!
[Total: 3 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

Magento 2: Add Quantity Increment and Decrement on Category Page

Hello Magento Friends, In this blog, we will discuss about adding quantity increment and decrement…

16 hours ago

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

4 days ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

6 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

6 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

7 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

1 week ago