voodish logo
tweet rss book of faces Linked In

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

[sourcecode language=’css’]
$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() ;
}
?>
[/sourcecode]

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

[sourcecode language=’css’]
$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 ‘www.summitjunkie.co.uk/mountains

Related Articles

Comments RSS Feed

33 Comments

  1. 9sixes June 24, 2010

    I’ve been hunting “everywhere” for this! Thanks a million!! :D

  2. Go to Top of the page

  3. Leanne September 7, 2010

    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

  4. Go to Top of the page

  5. admin September 7, 2010

    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.

  6. Go to Top of the page

  7. Pat October 14, 2010

    I’ve been looking everywhere for this!!!

    Thanks heaps!

  8. Go to Top of the page

  9. admin October 14, 2010

    Glad it was helpful Pat :)

  10. Go to Top of the page

  11. Confused October 14, 2010

    Could you explain where this code needs to be inserted please.

  12. Go to Top of the page

  13. admin October 14, 2010

    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

  14. Go to Top of the page

  15. Rick October 20, 2010

    Thanks for the great script! Is there a way to have the subcategories be sorted by the position they are placed in through the Manage Categories section? The categories seem to lose their sorting ability when using this script. Thanks…

  16. Go to Top of the page

  17. Beauty-Presence December 13, 2010

    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

  18. Go to Top of the page

  19. Beauty-Presence December 13, 2010

    oops I posted on the wrong place, this works nicely!

  20. Go to Top of the page

  21. Animesh Sheolikar January 27, 2011

    Thanks .It was beautiful and effortless coding

  22. Go to Top of the page

  23. Tony February 7, 2011

    @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
    }

  24. Go to Top of the page

  25. Matthew Galvin March 15, 2011

    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

  26. Go to Top of the page

  27. Caleb April 21, 2011

    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?

  28. Go to Top of the page

  29. vignesh April 28, 2011

    How can i arrange the Categories by ASC order by name..

  30. Go to Top of the page

  31. vignesh April 28, 2011

    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.

  32. Go to Top of the page

  33. admin April 28, 2011

    To add a sort to the list, try using the code found in this thread:
    http://www.magentocommerce.com/boards/viewthread/227052/

  34. Go to Top of the page

  35. Power Balance June 9, 2011

    Display SubCategory of current Parent category, i had try, that’s good help me.

  36. Go to Top of the page

  37. Top Englsih Songs June 10, 2011

    It works like a charm, Thanks!!

  38. Go to Top of the page

  39. Richard June 16, 2011

    Worked like a charm, thanks!

  40. Go to Top of the page

  41. HFE July 5, 2011

    good post dude thanks

  42. Go to Top of the page

  43. tarek November 29, 2011

    hi, I would like if u can add to this code the possibility of a toolbar for navigating between sub-categories pages if it will be viewed in more than one page. And thank you.

  44. Go to Top of the page

  45. Charles December 19, 2011

    Thanks so much have been working on a few different ways to accomplish this for last few hours. This is great!

  46. Go to Top of the page

  47. Michael January 20, 2012

    Great code! Worked like a charm.

    Is it possible to highlight the current subcategorie?

    Thanks!

  48. Go to Top of the page

  49. Shweta February 13, 2012

    i applied that code but its not working.
    Is that any configuration from backend.

  50. Go to Top of the page

  51. NW February 20, 2012

    Hallo,

    Thx you for code.

    The code come forward but without design, how can I get the design forward on the subcategorie like on the categorie?

    frontend categorie: http://timmco.dk/download/undermenumeddesign.jpg

    frontend subcategorie: http://timmco.dk/download/menuudendesign.jpg

    My earlier code, but without left menu in subcategories is here: http://timmco.dk/download/kodemeddesign.jpg

    hobe some one can help =)

  52. Go to Top of the page

  53. Juliana March 10, 2012

    Thank you for this code. It’s work perfect.

  54. Go to Top of the page

  55. Tim March 13, 2012

    Thank you for code, works great.

    Thanks!

  56. Go to Top of the page

  57. vivek shingala May 3, 2012

    thanx thanx thanx…
    helped a lot…
    keep it up…!!

  58. Go to Top of the page

  59. Sergiu June 8, 2012

    Thanks man, great post.

  60. Go to Top of the page

  61. Michel July 4, 2012

    When I look at entering a iten “in search” appears the following error

    Fatal error: Call to a member function getParentId () on a non-object in template / catalog / layer / view.phtml on line 41

    Can anyone help me?

    Thanks

  62. Go to Top of the page

  63. Praful Rajput August 13, 2012

    Really very helpful script for me…

    I used it in my module…

    thanks!!!!

  64. Go to Top of the page

  65. jyotiranjan August 21, 2012

    thanks …………..the above code is working for only current subcategory not the whole tree structure…….i want all the parent but if i will click parent that parent will only open except all other that means all other parent will display but in closed maner
    that is
    +1
    +11
    +11
    +111
    +222
    +2
    +22

    getStoreCategories();
    $current_cat = $obj->getCurrentCategory();
    $current_cat = (is_object($current_cat) ? $current_cat->getName() : ”);

    foreach ($store_cats as $cat) {
    if ($cat->getName() == $current_cat) {
    echo ‘getCategoryUrl($cat).’”>’.$cat->getName().”\n\n”;
    foreach ($obj->getCurrentChildCategories() as $subcat) {
    echo ‘getCategoryUrl($subcat).’”>’.$subcat->getName().”\n”;
    }
    echo “\n\n”;
    } else {
    echo ‘getCategoryUrl($cat).’”>’.$cat->getName().”\n”;
    }
    }
    ?>

  66. Go to Top of the page

Leave a comment