Removing Header and Footer Only from the Home Page in Magento 2
In Magento 2, you can remove the header and footer only from the home page using a combination of theme customization and layout updates. Here’s a step-by-step guide to achieve this without affecting other pages.
Understanding the Default Layout Structure
Magento 2 uses XML layouts defined in app/design/frontend/<vendor>/<theme>/layout folder to determine which elements are rendered on each page. The default.xml file controls the basic structure of all pages, including the header and footer.
Creating a New Layout File for the Home Page
To only remove the header and footer from the home page, you need to create a new layout XML file that inherits from the default and then modify it accordingly.
- Navigate to
app/design/frontend/<vendor>/<theme>/layout. - Create a new file called
homepage.xml.
Overriding the Default Layout for the Home Page
In your theme’s di.xml (usually located at app/design/frontend/<vendor>/<theme>/etc/di.xml), you need to configure Magento to use the new layout file for the home page.
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Theme\Model\View\Design">
<arguments>
<argument name="specificLayouts" xsi:type="array">
<item name="homepage" xsi:type="string">homepage</item> <!-- Change this to match your homepage layout file -->
</argument>
</arguments>
</type>
</config>
Replace homepage with the handle of your home page if it’s different.
Removing Header and Footer in the New Layout File
Open the newly created app/design/frontend/<vendor>/<theme>/layout/homepage.xml file.
<!DOCTYPE layout>
:layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Theme:etc/layout.xsd">
<referenceBlock name="header.container" remove="true"/>
<!-- Remove the footer block -->
<referenceBlock name="footer.container" remove="true"/>
</layout>
In this XML file, you are explicitly removing both the header and footer blocks using the remove attribute.
Testing the Changes
After making these changes:
- Clear your Magento cache:
php bin/magento cache:flush. - Check your home page. The header and footer should now be removed.
- Navigate to any other page, like a product or category listing. The header and footer should still be present.
This approach provides a clean separation of concerns and ensures that the changes are limited only to the home page as requested in the question.
Remember to replace <vendor> and <theme> with your actual theme details throughout this guide.
For more advanced customizations, consider using LaravelCompany’s Magento 2 Theme Development Guide for a comprehensive approach to theme development in Magento 2.
Conclusion
Removing the header and footer only from the home page in Magento 2 is possible by leveraging layout overrides and specific layout files. By following this guide, you can achieve the desired result without affecting other pages or stores within your multi-store setup.
Ensure you test these changes thoroughly to avoid any unintended side effects. If you encounter any issues, consider reaching out to a professional Magento development agency for further assistance.