Change sort order of sub categories in magento

Change sort order of sub categories in magento

July 30, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Change sort order of sub categories in magento

Changing the Sort Order of Sub Categories in Magento

Sorting sub categories on your Magento storefront can be a crucial aspect for enhancing user experience and driving sales. If you’re struggling to change the sort order of your sub categories, don’t worry—I’ll guide you through a step-by-step solution.

Understanding the Problem

You’ve tried adding ->addAttributeToSort('position', 'asc') in your code, but it’s not working as expected. The issue lies in how Magento handles category sorting by default and the structure of your current code.

By default, Magento sorts categories by name (sort_order) in ascending order (ASC). If you want to sort them by a custom attribute like position, you need to ensure that:

  1. The position attribute is available for the category.
  2. You’re using the correct model and method to apply sorting.

Fixing the Sort Order

To fix this, we’ll modify your existing code to properly handle category sorting by the position attribute.

Step 1: Create or update the position attribute

First, ensure that you have a position attribute available for your categories. You can create this attribute under Admin > Catalog > Attributes > Manage Categories > Add New Attribute.

Set the following options:

  • Attribute Code: position
  • Catalog Input Type for Store Owner: Text Field
  • Sort Order: Choose an appropriate value

Save the changes and apply them to all stores.

Step 2: Modify your code

Now, let’s modify your existing code to properly sort categories by the position attribute. Here’s the updated code:

<?php
// Get the current category
$_cat = new Mage_Catalog_Block_Navigation();
$currentCat = $_cat->getCurrentCategory();

// Get subcategories of the current category with sorting applied
$subCats = Mage::getModel('catalog/category')
    ->load($currentCat->getId())
    ->getChildrenCategories()
    ->addAttributeToSort('position', 'asc');

// Iterate through sorted subcategories
if ($subCats) {
    foreach ($subCats as $subCat) {
        if ($subCat->getIsActive()) {
            // Your category rendering logic here
        }
    }
}
?>

Explanation of Changes

  1. We used getChildrenCategories() instead of loading the children categories manually.
  2. Added ->addAttributeToSort('position', 'asc') to sort the subcategories by the position attribute in ascending order.

Now, when you run this code on your Magento store, it should display the subcategories sorted by their position values.

Best Practices

  • Always ensure that the position attribute is populated for each category. If not, they will be sorted by name (sort_order) instead.
  • Use clear and descriptive names for your custom attributes to avoid confusion.
  • Keep your code organized and well-documented for easier maintenance.

Conclusion

Changing the sort order of sub categories in Magento requires a proper understanding of how category sorting works and the correct use of models and methods. By following this guide, you should now be able to successfully sort your sub categories by a custom attribute like position.

Remember to test your implementation thoroughly on different devices and browsers to ensure consistent results.

If you’re looking for more advanced Magento solutions or need professional assistance with your store’s development, feel free to contact us at https://laravelcompany.com.

Happy coding!

Catalog Admin Orders Errors

Our network