Page Ranking Tool

All Category Listing In Magento

Some time we wants to display all categories in homepage or any CMS pages in Magento. There are many different ways to get the category listing. I will show you how you can get list of all categories of your Magento Shop.

1. Display all the Magento Categories(Active/Inactive)

The following code will fetch all categories (both active and inactive) that are presented in your Magento Store.

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

2. Display All Active Categories

The following code will fetch all active categories that are present in your Magento Store. Filtering the inactive categories.

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

3. Display Active Categories Of Any Particular Level in Magento

The following code will fetch all active categories of certain/specific level. Here, I have chosen level 1. Also sorting categories by name.

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

4. Display Store Specific Categories

The following code will fetch all 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);

5. Display Top level categories only in Magento

The following code will fetch all the Top level categories only

<?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) ?>">
<?php echo $_category->getName() ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>

6. Display Top Level Categories as well as ALL Subcategories in Magento

The following code will fetch all the Top Level Categories as well as ALL Subcategories

<?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; ?>

7. Display Subcategories only for the Current Top Category

The following code will fetch all 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; ?>

Hope this will be helfull. Thanks

Comments
  1. Hemant

    but where to put all the code ?

  2. Hemant

    i want . Display Top Level Categories as well as ALL Subcategories in Magento but wher to put this code ???i mean which file ? plz give ans ASAP

    • admin

      For the top menu/navigation use app/design/frontend/base/default/template/page/html/topmenu.phtml or app/design/frontend/default/Yourcustom_themeName/template/page/html/topmenu.phtml, if you are working with the custom theme.

      Thanks

      • Vina

        That’s a sensible awensr to a challenging question

      • Buffee

        God, I feel like I sholud be takin notes! Great work

  3. Prince

    Greetings from California! I’m bored to death at work so I decided to check out your blog on my iponhe during lunch break. I enjoy the info you provide here and can’t wait to take a look when I get home. I’m amazed at how quick your blog loaded on my phone .. I’m not even using WIFI, just 3G .. Anyways, superb site!

  4. Infinity

    Unparalleled accuracy, unequivocal cilatry, and undeniable importance!

  5. moringa leaves

    Nice, thats just what I was looking for .

  6. khalid

    Hello, im new in learning magento, your tutorial is a good enlightement lamp in my dark way
    Thank you !

  7. nice bro it’s working great

ADD YOUR COMMENT

*