Magento Sort Categories Alphabetically

Magento Sort Categories Alphabetically

September 4, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento Sort Categories Alphabetically

Displaying Magento Categories Alphabetically

Recently, I was tasked with displaying top-level categories and their sub-categories on the left navigation menu of a Magento site, sorted alphabetically. The provided code was able to display the categories but not in the desired order.

Identifying the Issue

The issue lies in how Magento’s built-in category helper (catalog_category) retrieves and sorts the categories. By default, it uses a complex algorithm involving product count and other factors for sorting. To achieve an alphabetical sort, we need to use a different approach.

Here’s the original code provided:

<ul id="demo1" class="nav">
<?php $helper = Mage::helper('catalog/category') ?>
<?php $categories = $helper->getStoreCategories() ?>
<?php foreach ($categories as $_category): ?>
    <?php echo $this->drawItem($_category) ?>
<?php endforeach ?>
</ul>

Solving the Problem

To display categories alphabetically, we can use Magento’s core block classes instead of the category helper. The Mage_Catalog_Block_Navigation class contains a method called getCachedCategories() which retrieves all top-level categories and sorts them by name.

First, let’s create an array to hold our sorted categories:

$categories = Mage::getModel('catalog/category')
    ->getCollection()
    ->addAttributeToSelect('name')  // Select only the 'name' attribute for performance
    ->addFieldToFilter('parent_id', 1)  // Filter by parent ID to get top-level categories
    ->setOrder('name', 'asc');

Then, we can loop through this collection and build our navigation menu:

<ul id="demo1" class="nav">
<?php foreach ($categories as $_category): ?>
    <li>
        <a href="<?php echo $this->getUrl($_category->getUrlPath()) ?>"><?php echo $_category->getName() ?></a>
    </li>
<?php endforeach ?>
</ul>

Displaying Sub-Categories

To display sub-categories under their parent category, we can use the getChildrenCategories() method:

<ul id="demo1" class="nav">
<?php foreach ($categories as $_category): ?>
    <li>
        <a href="<?php echo $this->getUrl($_category->getUrlPath()) ?>"><?php echo $_category->getName() ?></a>
        <?php if ($_category->hasChildren()): ?>
            <ul>
                <?php foreach ($_category->getChildrenCategories() as $_subCategory): ?>
                    <li><a href="<?php echo $this->getUrl($_subCategory->getUrlPath()) ?>"><?php echo $_subCategory->getName() ?></a></li>
                <?php endforeach ?>
            </ul>
        <?php endif ?>
    </li>
<?php endforeach ?>
</ul>

Conclusion

Displaying Magento categories alphabetically requires a different approach than the built-in category helper. By using Mage::getModel('catalog/category')->getCollection() and filtering by parent ID, we can retrieve top-level categories sorted by name.

For sub-categories, the getChildrenCategories() method allows us to display them under their respective parents.

Remember to replace the placeholder URLs with your actual Magento URLs. If you’re looking for more advanced customization or optimization of your Magento site, feel free to explore our services at Laravel Company.

Catalog Cache Performance Orders

Our network