I was just trying to find a code that can get current category and all the parent categories, also if there is a way to get the level of the categories to have a control over them. I have a code but it gets only the parent of the current category:

    $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);
            echo '<li>';
            echo '<a style="color:green" href="'.$cat->getURL().'">';            
            echo $cat->getName();
            echo '</a>';
            echo '</li>';



3 Answers 3


let's say that $category is your current category.
You can call $category->getParentCategories() and you will get an instance of Mage_Catalog_Model_Resource_Category_Collection.
You can loop through it and get each category in the tree branch.
You should be able to call ->getLevel() on each of these categories to see how high or low they are in the tree.

You can do this to loop through all the parent categories

foreach ($category->getParentCategories() as $parent) {
    //do something with $parent.
    if ($parent->getLevel() == 2) {
        //then $parent is a top level category
  • Hi can you please give a bit more info I am all dazed here. Commented Sep 2, 2015 at 5:57
  • See my edit. I'm not sure what more you need.
    – Marius
    Commented Sep 2, 2015 at 6:00
  • Yes that is exactly what i need the thing is i need to keep the level printed even if its the next one. Like this page basically i need a drop down search like this: allbatteries.co.uk/battery/car-battery.html Commented Sep 2, 2015 at 6:09
  • Ok ok sounds like i asked way too much. Just tell me how to make this work for the moment its not getting anything: if ($parent->getLevel() == 3) { echo $category->getName(); } Commented Sep 2, 2015 at 6:30

Given current category:

$currentCat = Mage::registry('current_category');

You can get all parent ids by the path, which is in the form 1/2/3/4, where in this example 1 is the system root category (this is actually always 1), 4 the current category 3 the immediate parent and 2 the second parent (the root category of the store)

$parentIds = explode('/', $currentCat->getPath());

Now $parentIds is an array with [2,3].

  • If you want to load the categories and not include the store root category, please refer to @Marius answer instead. Commented Sep 1, 2015 at 13:04
  • Your's and Marius's answers are great but i need to get category names urls too. Commented Sep 1, 2015 at 13:08
  • When you loop (foreach) over $category->getParentCategories(), you should be able to use getName() and getUrl() on them Commented Sep 1, 2015 at 13:10
  • Alright the Array ( [0] => 2 [1] => 3 [2] => 7 ), printing nicely but (foreach) crashing the page. Commented Sep 1, 2015 at 13:18
  • I was refering to Marius' answer ;) Mine only gives you the ids, which you could use to filter a category collection but using getParentCategories() is more straightforward and you should do it Commented Sep 1, 2015 at 13:20

Replace your first line:

$category = Mage::getSingleton('catalog/layer')->getCurrentCategory();


$category = Mage::getSingleton('catalog/layer')->getCurrentCategory();
$category = checkCategoryLevel($category);
function checkCategoryLevel($category) {
    if( $category->getLevel() == 2) {
        foreach ($category->getParentCategories() as $parent) {
            if( $category->getId() != $parent->getId()) {
                return Mage::getModel('catalog/category')->load($parent->getId());

If it works, place "checkCategoryLevel($category)" somewhere in block file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.