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

Aug 18, 20162607
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:

Partner With Us Let's discuss how to grow your business. Get a Free Quote.
Talk to Andrey

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.

prestashop themes

PrestaShop 1.6 - 1.7 themes

Take your online store design to the new level with BelVG PrestaShop themes for 1.6 and 1.7 versions

Visit the store

Need a ready-made and quality Magento solution? Visit BelVG store and find the one to suit your store the most.


Partner With Us Looking for a partner to grow your business? We are the right company to bring your webstore to success. Talk to Andrey

5 Comments

  1. Hi, could you please tell me where should I place this code:

    homepage-slider

    Is it inevitable or can I use it instead of this one:

    ???

  2. Dear Mari,

    it’s possible that I misunderstood the issue; could you please clarify the question?

    Kindly,
    Iryna

Post a new comment

top
BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *