Add active class to current subcategory

Add active class to current subcategory

October 8, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Add active class to current subcategory

Styling Active Subcategories in Magento

When working with categories in Magento, it’s common to want to style the active subcategory differently from its siblings. This can help guide users through your site’s navigation. Let’s explore how you can add an active class to the current child category.

Identifying the Current Category and Its Parent

In your provided code snippet, you’re correctly identifying both the current parent category and its children using Magento’s registry and model loading functions:

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

if ($currentCat->getParentId() == Mage::app()->getStore()->getRootCategoryId()) {
    $loadCategory = $currentCat;
} else {
    $loadCategory = Mage::getModel('catalog/category')->load($currentCat->getParentId());
}

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

This setup allows you to loop through the subcategories of the current category or its parent, depending on whether it’s a top-level category.

Checking for Active Subcategories

You’re also correctly filtering out inactive categories using $cat->getIsActive(). This ensures only active, accessible categories are displayed in your navigation menu:

foreach ($subCategories as $subCategoryId) {
    $cat = Mage::getModel('catalog/category')->load($subCategoryId);
    if ($cat->getIsActive()) {
        echo '<li><a href="' . $cat->getURL() . '">' . $cat->getName() . '</a></li>';
    }
}

Adding an Active Class to the Current Subcategory

To style the current subcategory, you need to compare it with the $currentCat variable. Magento stores the current category in this registry, so we can use a simple comparison:

foreach ($subCategories as $subCategoryId) {
    $cat = Mage::getModel('catalog/category')->load($subCategoryId);
    if ($cat->getIsActive()) {
        // Check if this subcategory is the same as the current one
        if ($cat->getId() == $currentCat->getId()) {
            echo '<li class="active"><a href="' . $cat->getURL() . '">' . $cat->getName() . '</a></li>';
        } else {
            echo '<li><a href="' . $cat->getURL() . '">' . $cat->getName() . '</a></li>';
        }
    }
}

In this modified loop, we’re adding the active class to the list item (<li>) if the current subcategory matches the $currentCat. This allows you to style the active category differently using CSS.

Styling the Active Category

To make use of the added active class, you can create a stylesheet rule in your Magento theme. For example:

.active {
    font-weight: bold;
    color: #ff6347; /* Change this to your desired active style */
}

Conclusion

Adding an active class to a current subcategory is a straightforward process that involves comparing the current category with each subcategory during the loop. By following the approach outlined above, you can easily add visual cues to guide users through your site’s navigation.

Remember, proper styling of navigational elements not only enhances user experience but also contributes to better accessibility and SEO.

For more advanced Magento customizations, consider exploring our services at Laravel Company. Our team specializes in Magento development, ensuring your e-commerce platform is optimized for both customers and your business needs.

Catalog

Our network