Customizing Magento 2 Footer Links: A Step-by-Step Guide
If you’re struggling to modify your Magento 2 footer links, you’re not alone! The default setup can be quite confusing for those accustomed to the older version. In this guide, I’ll walk you through a clean and safe approach to add more footer links, sort them into blocks with headings, without touching any core PHP files.
Understanding the Footer Structure
Magento 2 uses XML configuration to manage most of its layout, including the footer. This is great news for us because it means we can make changes using XML only, keeping our hands off those intimidating PHP files.
The default footer structure is defined in the vendor/magento/module-theme/view/frontend/layout/default.xml file. It consists of a single container called footer-container, which contains four link blocks named after the regions they occupy (footer_links, additional_links, etc.).
Creating Custom Footer Blocks
To add more links with headings, we need to create custom XML for our new blocks. Here’s an example of how to create a block called “My New Footer Links”:
- Create a file at
<your-theme-folder>/Magento_Theme/layout/default.xml. - Add the following content inside the
<layout>tags:
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="footer-container">
<block class="Magento\Framework\View\Element\Template" name="my_new_footer_links"
template="Magento_Theme::html/my_new_links.phtml"/>
</referenceContainer>
</body>
</page>
Replace <your-theme-folder> with the path of your current theme folder.
- Create a new PHTML file named
html/my_new_links.phtmlin the same location and add your custom HTML:
<div class="block block-last">
<div class="block-title">My New Footer Links</div>
<ul>
<li><a href="#">Link 1</a></li>
<li><a href="#">Link 2</a></li>
<!-- Add more links here -->
</ul>
</div>
Sorting Links into Existing Blocks
If you want to add new links directly into the existing blocks (like footer_links), you can do so by modifying the core XML. However, it’s better to avoid this for two reasons:
- Any changes to core files are overridden when you update Magento.
- It violates the “do not modify core files” rule.
A safer approach is to create new blocks as shown above and then move them around using layout updates. This keeps your customizations separate from the core, making it much easier to maintain and upgrade your site in the future.
Conclusion
Customizing Magento 2 footer links may seem daunting at first, but once you understand how XML layouts work, it becomes quite straightforward! By creating new blocks and managing them through layout updates, you can add more links, sort them into blocks with headings, all while keeping your hands off those core PHP files.
Remember to always back up your changes before making any modifications. For more detailed guides on Magento 2 development best practices, consider checking out the articles at Laravel Company’s Magento Blog.