0

How would you create a new referenceContainer in Magento 2 on home page only?

2 Answers 2

1

Create a new layout named cms_index_index into app/design/frontend/<vendor>/<package>/Magento_CMS/layout

You will then be able to reference what you need inside it, by example:

<?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">
    <referenceContainer name="CONTAINERNAME">
        // stuff
    </referenceContainer>
</page>
-1

Inside your module layout default.xml

Here i have taken header.container as a reference and added a new container inside.

<?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">
    <referenceContainer name="header.container">
        <container name="header.containertwo" as="header_containertwo" label="Page Header Container"  htmlTag="header" htmlClass="header-mini-container" after="header.panel.wrapper"/>
    </referenceContainer>
<referenceContainer name="header.containertwo">
    <block class="Magento\Framework\View\Element\Template" name="header.mini.container" template="Vendor_Module::test.phtml"/>
</referenceContainer>
</page>

In the test.phtml

<div style="border:1px solid red;" id="test123"><p>This is a test</p></div>

enter image description here

2
  • Wouldn't this add this container to all pages? Only need it on home page.
    – Devtype
    Sep 21, 2016 at 11:14
  • If you want to add only specific page , Go to specific cms page (in your case it is home page) Design -> Layout Update XML -> Add the Xml code. Sep 21, 2016 at 11:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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