Site icon MageComp Blog

Magento 2: Call Custom phtml file on all CMS Pages

Call Custom phtml file On All CMS Pages In M2

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!

Exit mobile version