3

I want to list all the categories in footer. Need the solution for both with and without sub-categories. something like this.

enter image description here

2 Answers 2

3

You can get the list of all the categories along with the url using the below code

This will display all top level categories:

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

To display all top level and subcategories use below code:

<?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() ?>
                </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) ?>">
                                    <?php echo $_subcategory->getName() ?>
                                </a>
                            </li>
                        <?php endforeach; ?>
                    </ul>
                <?php endif; ?>
            </li>
        <?php endforeach; ?>
    </ul>
<?php endif; ?>
9
  • where do i have to put this code? Commented Oct 9, 2014 at 8:34
  • You can put this code in your footer file ie: footer.phtml in app/design/frontend/yourtheme/default/template/page/html/footer.phtml or you can create a new template file with new position and put it in there.
    – Dexter
    Commented Oct 9, 2014 at 8:37
  • put that code right at the end of footer.phtml but not working Commented Oct 9, 2014 at 8:41
  • what is the problem?? its working perfectly fine in my system
    – Dexter
    Commented Oct 9, 2014 at 8:42
  • no error, nothing changed, the frontend is same like before. you can have a look at zumastic.com Commented Oct 9, 2014 at 8:42
1

you can do something like:

$root_category_id =  Mage::app()->getStore()->getRootCategoryId();
$root_category = Mage::getModel('catalog/category')->load($root_category_id);

function getCategories($category, $withSubcategories = false)
{
    $children = $category->getChildrenCategories();
    foreach ($children as $cat)
    {
        echo $cat->getName()."<br>";
        if($withSubcategories){
            getCategories($cat, true);
        }
    }
}

end after that if you need only main categories without sub-categories, only call:

getCategories($root_category);

and if you need with sub-categories:

getCategories($root_category, true);

of course you can format it as you want. This is only example how to fetch and echo catgories

2
  • where do i have to put this code? Commented Oct 9, 2014 at 8:34
  • you can put it into: app/design/frontend/your-package/your-theme/template/page/html/footer.phtml Commented Oct 9, 2014 at 8:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.