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