3

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?

3

2 Answers 2

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>
6
  • 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 ???? Commented Aug 3, 2018 at 11:14
  • @BhaskarCPothineni no , it will call automatically Commented Aug 3, 2018 at 11:16
  • First we need to delete the default.xml, and create cms_index_index.xml in the same folder . is it correct ?? Commented Aug 3, 2018 at 12:26
  • @BhaskarCPothineni no , both files were called at a same time Commented Aug 3, 2018 at 12:27
  • Okay Bro , I will try that and inform to you. jst give me 10 min.. Commented Aug 3, 2018 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>
3
  • 1
    The Theme Which i was created, that e doesn,t have the Magento_cms folder. Commented Aug 3, 2018 at 11:15
  • You need to create that as Magento_Cms/layout and then create with xml with cms_index_index.xml
    – kunj
    Commented Aug 3, 2018 at 11:16
  • @BhaskarCPothineni, I have updated answer.
    – kunj
    Commented Aug 3, 2018 at 11:22

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