3

This question already has an answer here:

I'm trying to display some static content in home page using default.xml.

But that content is loading in all pages of magento2.

I want the static content on home page only.

Can anyone help me to solve this issue using default.xml?

marked as duplicate by Piyush, liyakat, Dhiren Vasoya, Priyank, Manashvi Birla Aug 3 '18 at 14:12

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1

On same location create cms_index_index.xml and add your code init.

for example :

<?xml version="1.0"?>

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <block class="yourBLockClass" name="YourBLockName" template="YourPhtmlPath.phtml">
            </block>
        </referenceContainer>
    </body>
</page>
  • i created that file cms_index_index.xml in that location but it is not taking. Whether I need to configure that cms_index_index.xml file some where else ???? – Bhaskar C Pothineni Aug 3 '18 at 11:14
  • @BhaskarCPothineni no , it will call automatically – Murtuza Zabuawala Aug 3 '18 at 11:16
  • First we need to delete the default.xml, and create cms_index_index.xml in the same folder . is it correct ?? – Bhaskar C Pothineni Aug 3 '18 at 12:26
  • @BhaskarCPothineni no , both files were called at a same time – Murtuza Zabuawala Aug 3 '18 at 12:27
  • Okay Bro , I will try that and inform to you. jst give me 10 min.. – Bhaskar C Pothineni Aug 3 '18 at 12:28
0

Try this.

Add your phtml file in cms_index_index.xml(/app/design/frontend/{theme vendor}/{themename}/Magento_Cms/layout/cms_index_index.xml) layout file because cms_index_index is handler which calls only called at home.

If you don't have Magento_Cms folder under your theme you need to create that with Magento_Cms/layout/cms_index_index.xml with below code.

<?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>
         {{Your XML code will here}}
    <body/>
</page>
  • 1
    The Theme Which i was created, that e doesn,t have the Magento_cms folder. – Bhaskar C Pothineni Aug 3 '18 at 11:15
  • You need to create that as Magento_Cms/layout and then create with xml with cms_index_index.xml – kunj Aug 3 '18 at 11:16
  • @BhaskarCPothineni, I have updated answer. – kunj Aug 3 '18 at 11:22

Not the answer you're looking for? Browse other questions tagged or ask your own question.