Ever tried to remove the main menu from the header in Magento 2? Maybe because you wanted a customized menu instead of the default one. Or you have a one-page website which doesn’t require menu at all. Or the client’s site did not require the menu altogether!
In such cases, you need to remove menu from header in Magento 2 and here’s the programmatic solution to do the same.
For example, you don’t want to distract a user on the checkout page when he is about to make a conversion on your site with a menu that may prompt him to check or browse more.
So, you can simply remove the main menu from the header and speed up the conversion process at the checkout.
Programmatic method to remove menu from header in Magento 2:
Add below code into XML file where you want to remove the navigation menu:
<referenceContainer name="header-wrapper"> <referenceBlock name="navigation.sections" remove="true" /> </referenceContainer>
To remove the main menu from all pages in Magento 2:
Create app\code\[Vendor]\[Module]\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"> <body> <referenceContainer name="header-wrapper"> <referenceBlock name="navigation.sections" remove="true" /> </referenceContainer> </body> </page>
That’s it.
Feel free to share the solution with Magento community via social media.
Thank you.