Here I wanted to show you how to build a custom Magento category listing block that you can use on your own Magento store. We will go over each step at getting a category listing of your magneto categories and even a sub category listing.
Here is the finished code for your new category listing block.
<?php $cats = Mage::getModel('catalog/category')->load(2)->getChildren(); $catIds = explode(',',$cats); ?> <ul> <?php foreach($catIds as $catId): ?> <li> <?php $category = Mage::getModel('catalog/category')->load($catId); echo $category->getName(); $subCats = Mage::getModel('catalog/category')->load($category->getId())->getChildren(); $subCatIds = explode(',',$subCats); ?> <?php if(count($subCatIds) > 1):?> <ul> <?php foreach($subCatIds as $subCat) :?> <li> <?php $subCategory = Mage::getModel('catalog/category')->load($subCat); echo $subCategory->getName(); ?> </li> <?php endforeach;?> </ul> <?php endif; ?> </li> <?php endforeach; ?> </ul>
And The XML Block Code:
<block type="catalog/navigation" name="catalog.category" template="catalog/navigation/category.phtml" />
NOTE:
After loading each category you have a whole array of different attributes you can use. If you wanted to make each category listing be a link, simply use the getUrl() function in a similar fashion:
<a href="<?php echo $category->getUrl()?>"> <?php echo $category->getName()?> </a>
Enjoy everyone!
Devin R. Olsen