voodish logo
Skip to: Content | Navigation | Site map

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

Comments RSS Feed

20 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. 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

  16. Go to Top of the page

  17. Beauty-Presence December 13, 2010

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

  18. Go to Top of the page

  19. Animesh Sheolikar January 27, 2011

    Thanks .It was beautiful and effortless coding

  20. Go to Top of the page

  21. 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
    }

  22. Go to Top of the page

  23. 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

  24. Go to Top of the page

  25. 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?

  26. Go to Top of the page

  27. vignesh April 28, 2011

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

  28. Go to Top of the page

  29. 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.

  30. Go to Top of the page

  31. 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/

  32. Go to Top of the page

  33. Power Balance June 9, 2011

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

  34. Go to Top of the page

  35. Top Englsih Songs June 10, 2011

    It works like a charm, Thanks!!

  36. Go to Top of the page

  37. Richard June 16, 2011

    Worked like a charm, thanks!

  38. Go to Top of the page

  39. HFE July 5, 2011

    good post dude thanks

  40. Go to Top of the page

Leave a comment