Adding Static CMS Pages to Navigation in Magento 2
If you’re working on an e-commerce website built using Magento 2, adding static CMS pages to your navigation menu can be a simple yet powerful way to enhance user experience and drive engagement. In this post, we’ll guide you through the process of integrating your custom CMS pages with the default store categories for a seamless navigation flow.
Understanding Magento’s Navigation Structure
Before diving into the implementation, let’s first understand how navigation works in Magento 2. The core navigation is divided into:
- CMS Menu: This includes static pages and content blocks.
- Category Menu: This contains product categories.
- Product Menu: This lists individual products.
By default, these sections are displayed separately in the top menu. To combine them, you’ll need to modify Magento’s XML layout files.
Step 1: Create a Static CMS Page
First, create your new static page:
- Log in to your Magento admin panel.
- Navigate to Content > Pages.
- Click on Add New Page.
- Fill in the required details (like title, content).
- Click Save.
Your new page will be added to the CMS menu but won’t appear in the main navigation yet.
Step 2: Add Custom XML Layout
To integrate your static page with the category menu, you’ll need to update Magento’s layout files:
- Locate your theme directory (usually app/design/frontend/{Vendor}/{Theme}).
- Open the default.xml file inside the layout subdirectory.
- Find or add the following block within the
<body>tag:
<block type="Magento\Cms\Block\Cms" name="cms_page_link">
<action method="setPageId">
<argument name="page_id" xsi:type="number">12</argument> <!-- Replace with your page ID -->
</action>
</block>
<referenceContainer name="top.links">
<block type="Magento\Cms\Block\Cms" before="-" name="cms_page_link" template="Magento_Cms::page/view.phtml"/>
</referenceContainer>
In this example, replace 12 with the ID of your created static page.
Step 3: Recompile and Refresh
After updating the XML layout:
- Run the following commands in SSH (if using a Linux server):
php bin/magento setup:upgrade
php bin/magento maintenance:disable
php bin/magento cache:clean
php bin/magento indexer:reindex
-
If you’re running the site locally, clear Magento’s cache (usually located at var/cache) and run
bin/magento setup:upgrade. -
Flush your browser cache to see the changes.
Step 4: Verify the Result
Now, when you visit your storefront, you should see your static CMS page integrated into the main navigation menu alongside the category links. The page will appear with a link title (based on its CMS page configuration) and open in a new tab when clicked.
Remember that adding too many non-product pages to the main menu can clutter it and confuse users. Use this feature judiciously for critical information or as part of your site-wide navigation hierarchy.
Conclusion
By following these steps, you’ve successfully expanded Magento 2’s default navigation capabilities to include custom CMS pages alongside product categories. This flexibility allows you to control how customers navigate your store, enhancing their overall experience and potentially driving more conversions.
If you found this guide helpful, check out Laravel Company’s extensive tutorials on Magento development for more advanced techniques and best practices in e-commerce site building.