Sorting categories in Magento according to the position in admin

Sorting categories in Magento according to the position in admin

July 30, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Sorting categories in Magento according to the position in admin

Sorting Magento Categories by Position: A Comprehensive Guide

Sorting categories is an essential task for any Magento store owner looking to optimize their website’s navigation and user experience. In this blog post, we will delve into the process of sorting your categories based on the position specified in the admin panel.

Understanding Category Sorting in Magento

By default, when you create a new category in Magento, it assigns an ID that determines the sort order. This is not ideal if you want to display your categories in a specific sequence unrelated to their creation date or ID.

The solution lies in utilizing the “Sort Order” field available for each category in the admin panel under Catalog > Categories > Edit Category.

Setting Up Your Custom Category Listing

First, let’s ensure we have our custom category listing block set up properly. If you haven’t done so already, follow this excellent tutorial by Devin Rolsen: Magento Custom Category Listing Block.

The provided code snippet fetches the children of a specific category (ID 3) and displays them in an unordered list. However, it sorts them based on the category ID.

<?php
$cats = Mage::getModel('catalog/category')->load(3)->getChildren();
$catIds = explode(',',$cats);
?>
<ul>
<?php foreach($catIds as $catId): ?>
    <li>
        <?php
            $category = Mage::getModel('catalog/category')->load($catId);
            echo '<a href="' . $category->getUrl() . '">';
            echo $category->getName() . '</a>';
        ?>
    </li>
<?php endforeach; ?>
</ul>

Sorting Categories by Position

To sort the categories based on the “Sort Order” field, we need to modify our code.

<?php
$rootCategory = Mage::app()->getStore()->getRootCategoryId();
$categories = Mage::getModel('catalog/category')->getCollection()
    ->addAttributeToSelect(array('name', 'url'))
    ->setOrder('position', 'asc');
?>
<ul>
<?php foreach($categories as $category): ?>
    <li>
        <?php
            echo '<a href="' . $category->getUrl() . '">';
            echo $category->getName() . '</a>';
        ?>
    </li>
<?php endforeach; ?>
</ul>

In this updated code:

  • We first fetch the root category ID.
  • Then, we create a collection of all categories using Mage::getModel('catalog/category')->getCollection(). To ensure our block displays only relevant information, we use ->addAttributeToSelect(array('name', 'url')).
  • Finally, we set the sorting order to “position” in ascending order with ->setOrder('position', 'asc').

Conclusion

Sorting your Magento categories based on position is a straightforward process once you understand how to manipulate the category collection. By following this guide and adjusting the provided code snippet, you can ensure that your website’s navigation reflects your desired category hierarchy.

Remember, consistency in sorting helps maintain a clean, organized site structure that positively impacts both user experience and search engine optimization (SEO). If you need further assistance with Magento development or e-commerce strategies, feel free to reach out to our team at Laravel Company.

Happy coding!

Catalog Performance Admin Configuration

Our network