Creating Custom Navigation with Magento Category Links in Version 1.9
As a beginner in the world of Magento e-commerce development, you’ve likely found yourself looking for ways to customize your site’s navigation menu. Specifically, you want to create two separate menus - a main menu and a mobile-specific menu - both populated with category links.
The good news is that this can be achieved within the confines of Magento 1.9 using some basic template modifications and layout updates.
Understanding the Current Setup
In your current setup, you’ve managed to customize the top navigation HTML for your main menu. This involves modifying the app/design/frontend/default/yourtheme/template/page/html/topmenu.phtml file. The core layout XML files responsible for this are located in the layout folder of your theme.
Creating a New Menu Block
To create the mobile-specific menu, we’ll first need to define a new block within one of our layout XML files. For simplicity’s sake, let’s add it to the twocolumns-left.phtml file, which is typically used for category pages.
Open up your chosen theme’s copy of this file (it should be located in app/design/frontend/default/yourtheme/layout) and find the main content block:
<reference name="content">
<block type="catalog/category_view" name="category.products" template="catalog/category/products.phtml"/>
</reference>
Directly beneath this, add your new mobile menu block:
<reference name="left">
<block type="cms/block" name="mobile_menu_block">
<action method="setBlockId"><block_id>your_mobile_menu_block_id</block_id></action>
</block>
</reference>
Replace your_mobile_menu_block_id with a unique identifier for your new block.
Populating the New Block
Next, you need to populate this new mobile menu block. This involves creating a custom category list and assigning it to our new block.
Open up the app/design/frontend/default/yourtheme/template/cms/block.phtml file and find the line that outputs the content of your CMS block:
<?php echo $this->getContent() ?>
Replace this with your own PHP code to create a custom category list. Here’s a basic example:
<?php
// Get current store id
$store_id = Mage::app()->getStore()->getId();
// Get all enabled categories for the current store
$categories = Mage::getModel('catalog/category')
->getCollection()
->addFieldToFilter('is_active', 1)
->addFieldToFilter('store_id', $store_id);
// Loop through each category and display it in a list item
foreach ($categories as $category) {
echo '<li><a href="' . $this->getUrl($category->getRequestPath()) . '">' . $category->getName() . '</a></li>';
}
?>
This code retrieves all enabled categories for the current store and displays them in an unordered list.
Conclusion
Creating custom navigation menus in Magento 1.9 involves a combination of layout modifications, template overrides, and basic PHP coding to generate dynamic content. By following this guide, you should now be able to create distinct main and mobile category menus for your site.
Remember, the key is understanding how Magento’s block and layout systems interact, which allows you to strategically place new content wherever needed within your storefront.
For more advanced navigation customization techniques in later versions of Magento, I recommend checking out our Magento Development Guide over at LaravelCompany.