How to Call .phtml Template Only on Homepage in Magento 2

How to Call .phtml Template Only on Homepage in Magento 2

Here is a small tip on how to call CMS static block on the homepage with a specific .phtml template in Magento 2.

Magento 2 allows easy creating of CMS static blocks. After you’ve created a CMS Block, you need to add this block into the layout.

In order to add the block on every page, you should use default.xml layout file from app/design/frontend/Vendor/Theme/Magento_Theme/layout directory, that will call block’s template on every page of your current theme.

If you need to place Static Block only on the homepage you should add directives into cms_index_index.xml layout file from the app/design/frontend/Vendor/Theme/Magento_Cms/layout directory. For those who just get started with Magento 2 let’s take a look at the detailed example.

For example, you have created CMS Block “Slider” which has the following ID “homepage-slider“. In order to use the slider block only on the Homepage, you should place your template (let’s call it slider.phtml) to the directory app/design/frontend/Vendor/Theme/Magento_Cms/templates of your current theme. Layout update file cms_index_index.xml should be placed into the app/design/frontend/Vendor/Theme/Magento_Cms/layout directory of your current theme.

How to call .phtml template only on Homepage in Magento2

Template file slider.phtml file will call your block:

Layout file cms_index_index.xml will call your template:

Another way to add CMS Block to the page is:

That’s it.

Check also how to create a custom Homepage Template in Magento 2.



1 comment

Post a new comment

top