Magento: display all level2 categories

Magento: display all level2 categories

September 8, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento: display all level2 categories

Displaying Level 2 Categories in Magento: A Comprehensive Guide

In Magento, handling multi-level category structures can be challenging but rewarding when you learn to navigate them effectively. One common requirement is to display only the level 2 categories from a given category on a custom block.

Understanding Your Category Structure

Before we dive into the code, let’s clarify your current setup:

  1. You have categories with up to 4 levels.
  2. The structure looks like this: level0category → level1category → level2category → last_level_category.
  3. You want to display only the level 2 categories when viewing a category at the ‘last level’.

Magento’s Category Hierarchy and Parent-Child Relationships

In Magento, categories are organized in a tree-like structure defined by their parent-child relationships. The root-level category (level 0) is the topmost node with no parent.

Each subsequent level represents a child of its parent. So:

  • Level 1 = Children of level 0
  • Level 2 = Children of level 1
  • And so on…

Detecting the Current Category Level

To achieve your goal, we need to identify the current category’s level within this hierarchy.

The Mage::registry('current_category') method retrieves the currently active category object. We can use its getParentId() method to navigate up the tree:

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

if ( $currentCat->getParentId() == Mage::app()->getStore()->getRootCategoryId() )
{
    // current category is a top-level category
    $loadCategory = $currentCat;
}
else
{
    // current category is a sub-(or subsub-, etc...)category of a top-level category
    // load the parent category of the current category
    $loadCategory = Mage::getModel('catalog/category')->load($currentCat->getParentId());
    // @TODO enhance for more nested category levels to display sub-categories
}

Loading and Displaying Level 2 Categories

Once we have loaded the top-level (level 1) parent category, we can retrieve its children using explode(',', $loadCategory->getChildren()).

The getChildren() method returns a comma-separated string of child category IDs. We explode this into an array to iterate through each level 2 category:

$subCategories = explode(',', $loadCategory->getChildren());

foreach ( $subCategories as $subCategoryId )
{
    $cat = Mage::getModel('catalog/category')->load($subCategoryId);

    if ($cat->getIsActive())
    {
        // Check if the current category is the same as the sub-category
        if ($currentCat->getEntityId() == $subCategoryId)
        {
            echo '<li style="display:none;">'.$cat->getName().'</li>';
        }
        else
        {
            echo '<a href="'.$cat->getURL().'">'.$cat->getName().'</a> <br>';
        }
    }
}

Conclusion and Next Steps

In this post, we’ve covered how to display level 2 categories in Magento. The key steps are:

  1. Identify the current category object.
  2. Detect its parent (level 1) category.
  3. Load the children of the parent (level 2 categories).
  4. Iterate through and display each active level 2 category.

Remember, this is a basic example to get you started. You may need to enhance it further based on your specific requirements, such as handling multiple nested levels or applying custom design elements.

To learn more about Magento development best practices, visit our Magento Development Guide at Laravel Company for in-depth tutorials and resources.

Catalog

Our network