Creating Custom Header Extending Magento Blank in Magento 2
Are you struggling to create a custom header for your Magento 2 theme using the Blank theme as a parent? You’re not alone! Let’s dive into how to properly modify the header structure and position it before the main content.
Understanding the Issue
The default layout.xml in the Magento Blank theme positions various blocks within the page wrapper. When you remove elements like header.container, they are still nested inside page.wrapper between main-content and footer. This is why your custom container appears after the main content instead of before it.
Solving the Problem
To create a custom header that precedes the main content, we need to make strategic changes in our theme’s layout.xml. Here’s how to do it:
- Remove unnecessary blocks: Start by removing the unwanted links and containers from the
bodysection of your layout.xml.
<referenceBlock name="register-link" remove="true" />
<referenceBlock name="authorization-link" remove="true" />
<referenceBlock name="wish-list-link" remove="true" />
<referenceBlock name="my-account-link" remove="true" />
<referenceBlock name="top.links" remove="true" />
<referenceBlock name="header.panel.wrapper" remove="true" />
<referenceBlock name="header.container" remove="true" />
- Define the custom header container: Next, define your new
custom.headercontainer inside thepage.wrapper. Set the HTML class and tag to control its appearance.
<referenceContainer name="page.wrapper">
<container name="custom.header" htmlClass="headerSection" htmlTag="div">
<!-- Your custom blocks will go here -->
</container>
</referenceContainer>
- Place your custom block: Now, place your custom navigation block inside the
custom.headercontainer.
<block class="Magento\Framework\View\Element\Template"
name="navigation.sections"
before="-"
template="Magento_Theme::html/sections.phtml">
</block>
- Set the main content to be nested: Finally, ensure that the
main-contentblock is nested inside your custom header container.
<referenceContainer name="custom.header" htmlClass="headerSection" htmlTag="div">
<block class="Magento\Framework\View\Element\Template"
name="navigation.sections"
before="-"
template="Magento_Theme::html/sections.phtml">
<!-- Your custom blocks will go here -->
</block>
<referenceBlock name="main-content" after="navigation.sections"/>
</referenceContainer>
Remember to Clear the Cache
After making these changes, don’t forget to clear your Magento cache to see the updates. You can do this by running:
php bin/magento cache:clean
Conclusion
Creating a custom header in Magento 2 with the Blank theme as a parent requires understanding how layout.xml and containers work together. By strategically removing unnecessary blocks, defining a new container for your custom header, and nesting the main content inside this container, you can achieve the desired layout.
Don’t hesitate to explore the vast resources available at LaravelCompany for more in-depth tutorials on Magento development and theme creation. Happy coding!