Your Ad Here

Monday, October 17, 2011

Display categories on home page in Magento

The code below does a couple of things, first it will get all the store’s categories - it then checks to see if they are active before continuing.

<?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 '<a href="'.$category->getURL().'">'.$category->getName().'</a>';
               
        $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();
        if($subCategory->getIsActive())
        {
            echo '<a href="'.$subCategory->getURL().'">'.$subCategory->getName().'</a>';
        }

        ?>
        </li>
        <?php endforeach;?>
        </ul>
        <?php endif;?>
        </li>
        <?php endforeach; ?>
        

No comments:

Post a Comment