Display Parent category and subcategories of current parent in Magento
How to show parent and sub category menu items of parent items in magento.
Display Parent Category
This also takes into consideration where you are in the system. For example if you are in a subcategory, then parent category of the subcategory will be displayed. However, if you are in a subcategory of DEFAULT CATEGORY, then the code checks for this and displays your current category.
We placed the following code into our template sidebar
e.g.: /app/design/frontend/default/your-bespoke-design-template/template/catalog/navigation/leftnav.phtml
<?php
$currentCat = Mage::registry('current_category');
//if Rootcategory display current category only
//this gets around the problem of displaying DEFAULT CATEGORY
if ( $currentCat->getParentId() == Mage::app()->getStore()->getRootCategoryId() )
//Display current category
echo $this->getCurrentCategory()->getName() ;
else
{
// Display ParentCategory of Current Category
echo $this->getCurrentCategory()->getParentCategory()->getName() ;
}
?>
Display SubCategory of current Parent category
Show Child categories based on current Parent Category; extremely useful for navigational purposes.
Again for those wanting subcategories of the current page, we used the following code:
e.g.: /app/design/frontend/default/your-bespoke-design-template/template/catalog/navigation/leftnav.phtml
<?php
$currentCat = Mage::registry('current_category');
if ( $currentCat->getParentId() == Mage::app()->getStore()->getRootCategoryId() )
{
// current category is a toplevel category
$loadCategory = $currentCat;
}
else
{
// current category is a sub-(or subsub-, etc...)category of a toplevel category
// load the parent category of the current category
$loadCategory = Mage::getModel('catalog/category')->load($currentCat->getParentId());
}
$subCategories = explode(',', $loadCategory->getChildren());
foreach ( $subCategories as $subCategoryId )
{
$cat = Mage::getModel('catalog/category')->load($subCategoryId);
if($cat->getIsActive())
{
echo '<a href="'.$cat->getURL().'">'.$cat->getName().'</a>';
}
}
?>
Live example
www.summitjunkie.co.uk/mountains
Related Articles
- 17.05.11: Currency drop down selector in header, Magento (3)
- 26.07.11: Backup Incoming and Outgoing mail for your domain EXIM (0)
- 23.06.11: Add logo to print popup in Joomla (0)
- 14.03.11: Turn off page titles globally in Joomla (0)
- 03.06.11: Yoo maps address, how to change address text (0)









Comments RSS Feed





I’ve been hunting “everywhere” for this! Thanks a million!!
Go to Top of the page
Thanks! This works on all Category pages but it is causing a fatal error on the homepage and all other pages not associated with a Category. I get this error:
Fatal error: Call to a member function getParentId() on a non-object…
Can you help?
Thanks
Go to Top of the page
Sorry this was built for use within the Magento category sections only.
We used different layouts to avoid any errors elsewhere; will take further R&D to get working across Magento.
Go to Top of the page
I’ve been looking everywhere for this!!!
Thanks heaps!
Go to Top of the page
Glad it was helpful Pat
Go to Top of the page
Could you explain where this code needs to be inserted please.
Go to Top of the page
We used it in the sidebar of one of our templates, so that sub-cats of the current cat would display:
/app/design/frontend/default/your-bespoke-design-template/template/catalog/navigation/leftnav.phtml
Example of it working:
http://www.summitjunkie.co.uk/mountains
Go to Top of the page
I get an error when I insert this code… sorry I don’t really know PHP so probably it’s very to fix, the error is at the end of the code snippet..
Parse error: syntax error, unexpected T_ENDFOREACH in \app\design\frontend\bp\bp-theme\template\catalog\category\view.phtml on line 76
Go to Top of the page
oops I posted on the wrong place, this works nicely!
Go to Top of the page
Thanks .It was beautiful and effortless coding
Go to Top of the page
@Leanne–You just need to add a conditional statement to prevent that code from running on non-category pages:
if (Mage::registry(’current_category’)){
//insert your category code here
}
Go to Top of the page
Any idea how to get this to return the subcats in their proper order?
This code displays them listed by id ASC and I want to order by position ASC
Go to Top of the page
Thanks for posting this code! Is there a way to apply an active class to the subcategory link? So when you are in the subcategory page it will be highlighted in the navigation?
Go to Top of the page
How can i arrange the Categories by ASC order by name..
Go to Top of the page
i have to display categories in the page same like this.
Example of it working:
http://www.summitjunkie.co.uk/mountains
Subcategories for mountains
Ben Nevis
Buachaille Etive Mor.. etc
Showing there in Ascending order by name.
Go to Top of the page
To add a sort to the list, try using the code found in this thread:
http://www.magentocommerce.com/boards/viewthread/227052/
Go to Top of the page
Display SubCategory of current Parent category, i had try, that’s good help me.
Go to Top of the page
It works like a charm, Thanks!!
Go to Top of the page
Worked like a charm, thanks!
Go to Top of the page
good post dude thanks
Go to Top of the page