Skip to content

Instantly share code, notes, and snippets.

@jcobb
Created July 10, 2012 06:45
Show Gist options
  • Save jcobb/3081631 to your computer and use it in GitHub Desktop.
Save jcobb/3081631 to your computer and use it in GitHub Desktop.
Custom Magento Category Navigation
<div class="category-navigation">
<?php
// get the root category id
$root_cat_id = Mage::app()->getStore()->getRootCategoryId();
// get the current category object and ID
$current_cat_obj = Mage::registry('current_category');
$current_cat_id = $current_cat_obj->getId();
// get the ID of the parent category from the current category
$parent_cat_id = $current_cat_obj->getParentCategory()->getId();
$parent_cat_obj = Mage::getModel('catalog/category')->load($parent_cat_id);
// get the sub categories of the current category
$sub_cat_obj = $current_cat_obj->getChildrenCategories();
if($current_cat_obj->hasChildren()){
// if the current category obj has children, use that
$menu_cats = $current_cat_obj->getChildrenCategories();
$menu_top_level = $current_cat_obj;
}else{
// else use the parent object
$menu_cats = $parent_cat_obj->getChildrenCategories();
$menu_top_level = $parent_cat_obj;
}
// top level details
$top_level_name = $menu_top_level->getName();
$top_level_url = $menu_top_level->getUrl();
$top_level_id = $menu_top_level->getId();
// is the top level the root?
if($top_level_id == $root_cat_id){
echo '<ul><li class="current">'.$current_cat_obj->getName().'</li></ul>';
}
else{
// start navigation
echo '<ul>';
// check if the top level is the current category and output accordingly
if($current_cat_id == $top_level_id){
echo '<li class="current">View All</li>';
}
else{
echo '<li><a href="'.$top_level_url.'">View All</a></li>';
}
// loop over all the sub category objects
foreach($menu_cats as $key => $sub_cat){
$cat_link = $sub_cat->getUrl();
$cat_name = $sub_cat->getName();
$cat_id = $sub_cat->getId();
// check if the sub category is the current category and output accordingly
if($current_cat_id == $cat_id){
echo '<li class="current">'.$cat_name.'</li>';
}
else{
echo '<li><a href="'.$cat_link.'">'.$cat_name.'</a></li>';
}
}
// end navigation
echo '</ul>';
}
?>
</div>
@jcobb
Copy link
Author

jcobb commented Jul 10, 2012

<block type="core/template" name="superwickedcustomnav" template="page/custom-cat-nav.phtml" before="cart_sidebar" />

Use the XML snippet above in the left reference of catalog.xml to make the custom template appear in the left sidebar of all category and product pages.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment