Categories: Magento

How To Get All Categories in Magento.?

Get All Categories in Magento
When you need to display all categories on homepage or any cms page then you can use below methods to work with different list of all Magento Categories, all active Magento Categories, get specific categories, Store Specific Categories, Top level categories, and Subcategories only for the Current Top Category for using them in your E-Commerce platform.


These are the following different ways to get the category listing on homepage or cms pages.

Magento Get All Categories Active and Inactive:

The below code will fetch all categories with both active and inactive on your magento store

$categories = Mage::getModel('catalog/category')
                    ->getCollection()
                    ->addAttributeToSelect('*');

Magento Get All Active Categories:

The below code will fetch all active categories that are present in your Magento Store.

$categories = Mage::getModel('catalog/category')
                    ->getCollection()
                    ->addAttributeToSelect('*')
                    ->addIsActiveFilter();

Magento Get Active Categories To Any Particular Level(Specific Category):

The below code will fetch all active categories of specific level and sorting categories by name.

$categories = Mage::getModel('catalog/category')
                    ->getCollection()
                    ->addAttributeToSelect('*')
                    ->addIsActiveFilter()
                    ->addLevelFilter(1)
                    ->addOrderField('name');

Magento Get Store Specific Categories:

The below code will fetch active store specific categories

getStoreCategories($sorted=false, $asCollection=false, $toLoad=true)

$helper = Mage::helper('catalog/category');

// sorted by name, fetched as collection
$categoriesCollection = $helper->getStoreCategories('name', true, false);

// sorted by name, fetched as array
$categoriesArray = $helper->getStoreCategories('name', false, false);

Magento Get All Top Level Categories and SubCategories:

The below code will fetch all the Top Level Categories as well as All Subcategories for your current magento store.

<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php $currentCategory = Mage::registry('current_category') ?>
<?php if (count($_categories) > 0): ?>
<ul>
<?php foreach($_categories as $_category): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
<?php echo $_category->getName() ?> //Top Level Category Listing
</a>
<?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
<?php $_subcategories = $_category->getChildrenCategories() ?>
<?php if (count($_subcategories) > 0): ?>
<ul>
<?php foreach($_subcategories as $_subcategory): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>"> 
//Sub Category Listing
<?php echo $_subcategory->getName() ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>

Magento Get Subcategories only for the Current Top Category:

The below code will fetch all the Subcategories only for the Current Top Category.

<?php $_currentCategory = Mage::register('current_category') ?>
<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php if (count($_categories) > 0): ?>
<ul>
<?php foreach($_categories as $_category): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_category) ?>" title="<?php echo $_category->getName() ?>">
<?php echo $_category->getName() ?>
</a>
<?php if ($_category->getId() == $_currentCategory->getId()): ?>
<?php $_subcategories = $_currentCategories->getChildrenCategories() ?>
<?php if (count($_subcategories) > 0): ?>
<ul>
<?php foreach($_subcategories as $_subcategory): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>" title="<?php echo $_subcategory->getName() ?>">
<?php echo $_subcategory->getName() ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>

Rohan pathak

Recent Posts

Modern Toys, Magical Moments: Why the Best Toy Shop in Noida Is More Than Just a Store

When it comes to children, there’s one universal truth: the right toy can spark imagination, build skills, and make memories…

6 months ago

Rediscovering Joy: A New Era of Creativity & Comfort in Toy Stores

In today’s digital age, where screens and gadgets dominate our children’s lives, there’s something heartwarming about a well-loved plush toy…

6 months ago

Unboxing Imagination: Discovering the Joy of Play at a Toy Store in Noida

In a world dominated by screens and fast-paced routines, it’s easy to forget the simple magic of a toy in…

6 months ago

Imagination Unboxed: Discover Joy at the Toy Shop in Delhi

In the heart of Delhi’s vibrant streets lies a world where imagination meets innovation — the magical universe of toys.…

6 months ago

Play with Purpose: Discovering the Ultimate Toy Store in Noida

When was the last time a toy truly amazed you—not just as a product, but as a thoughtful tool for…

6 months ago

From Tears to Toys: Exploring Modern Childhood through Delhi’s Favorite Toy Shop

In the digital age, the way we experience childhood has changed, but the essence remains the same—imagination, exploration, and joy.…

6 months ago