How to Load a .phtml File Only for Homepage in Magento 2

conditional-loadinghomemagento2phtml

I am trying to load a phtml file like this:

File /app/design/frontend/name/themename/Magento_Theme/layout/default.xml

    <referenceContainer name="page.top">
        <block class="Magento\Framework\View\Element\Template" name="startphoto" template="html/startphotos.phtml">
            <arguments>
                <argument name="section" xsi:type="string">homepage</argument>
                <argument name="position" xsi:type="number">0</argument>
            </arguments>
        </block>
    </referenceContainer>

I want to load it only on homepage, but it is still loading on every page. The arguments seems to be ignored.

Best Answer

As per as, Magento2, default.xml is call at every page.If you add this code at default.xml then it would be automatically call every pages of your current theme.

If you know about Magento 1.x handler concept then you can understand that default is a handle which is called at every pages.In Magento 2.X,every handler is individual layout files.

So if want to add a phtml file only at home page then you need to add that code at cms_index_index.xml (/app/design/frontend/name/themename/Magento_Cms/layout/cms_index_index.xml) layout file because of cms_index_index is handler which call only called at home

Related Topic