Get Categories and Its Sub Categories in Magento
Today I will show you how you can fetch magento categories and its subcategories. Use below code
<?php
$_helper = Mage::helper('catalog/category');
$_categories = $_helper->getStoreCategories();
if (count($_categories) > 0){
foreach($_categories as $_category){
$_category = Mage::getModel('catalog/category')->load($_category->getId());
$_subcategories = $_category->getChildrenCategories();
if (count($_subcategories) > 0){
echo $_category->getName();
echo $_category->getId();
echo $_category->getUrl();
foreach($_subcategories as $_subcategory){
echo $_subcategory->getName();
echo $_subcategory->getId();
echo $_subcategory->getUrl();
}
}
}
}
?>
To show only one category where 2 is your main category id
<?php
$category = Mage::getModel('catalog/category')->load(2);
$subcategories = $category->getChildrenCategories();
if (count($subcategories) > 0){
echo $category->getName();
foreach($subcategories as $subcategory){
echo $subcategory->getName();
}
}
?>