4

I am working on a new theme which is based on the Luma theme. I need to add a extra container to page header.my problem is that I can't identify how/where to add this.

Can anyone advise on what page i need to edit for this?

See the reference image

enter image description here

3 Answers 3

6

Add a container after="header.panel.wrapper" inside header.container. By the reference of the container add your template.

Inside your default.xml

<?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>

Out put enter image description here

Sample module

/app/code/Vendor/Module/registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_Module',
    __DIR__
);

/app/code/Vendor/Module/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Vendor_Module" setup_version="1.0.0" schema_version="1.0.0">
    </module>
</config>

/app/code/Vendor/Module/view/frontend/layout/default.xml

<?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>

/app/code/Vendor/Module/view/frontend/templates/test.phtml

<div style="border:1px solid red;" id="test123"><p>This is a test </p></div>
4
  • Do you mean the default.xml located in Magento_theme/layout? Also where do I add the test.phtml to Magento_theme/template/html ?
    – user159500
    Sep 18, 2016 at 8:47
  • OK, I've tried it and it's not showing...I've added it like this: <referenceContainer name="header.containertwo"> <block class="Magento\Framework\View\Element\Template" name="header.mini.container" template="Vendor_Module::test123.phtml"/> </referenceContainer> ... test123.html is in myTheme/Magento_theme/Templates directory...that right?
    – user159500
    Sep 18, 2016 at 12:13
  • I'm a bit confused about the "code" directory as mine is currently empty. For example my registration.php is in myVentor/myTheme/ directory. Do I still have to add these files to the code directory?
    – user159500
    Sep 18, 2016 at 16:40
  • @user159500 would you please share with us as to what did you do exactly? I'm also confused about the "code" directory as I do not have it. Magento 2.2.2 Feb 14, 2018 at 0:58
0

The simplest way is

Step one: Create a block in path:

app/design/frontend/Vendor/Theme/Magento_Theme/layout/default.xml

<referenceContainer name="header.container"><block class="Magento\Framework\View\Element\Template" name="custom_header" as="custom_header" template="Magento_Theme::html/customheader.phtml" before="-"/>

Step 2: Create phtml page path:

app/design/frontend/Vendor/Theme/Magento_Theme/templates/html/customheader.phtml

<h1>hello magento</h1>
0

Simple Way:

  1. Create a block in path:app/design/frontend/Your_Vendor/Your_Theme/Magento_Theme/layout/default.xml

Content will be :

<referenceContainer name="header.container"><block class="Magento\Framework\View\Element\Template" name="custom_header" as="custom_header" template="Magento_Theme::html/customheader.phtml" before="-"/></referenceContainer>
  1. Create custom.phtml page in path:app/design/frontend/Your_Vendor/Your_Theme/Magento_Theme/templates/html/customheader.phtml

content will be:

<h1>Your_Content_here</h1>

Your Answer

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

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