Magento getUrl not working with catalog/category object?

Kayla picture Kayla · May 5, 2011 · Viewed 7.5k times · Source

I've been able to instantiate a category object to retrieve its name, but when I use the getUrl method it isn't returning a URL to the category listing page, or anything at all

<?php
$children = Mage::getModel('catalog/category')->getCategories(3);
foreach ($children as $category):
            echo '<li><a href="' . $category->getUrl() . '">' . $category->getName() . '</a></li>';
        endforeach;
?> 

The code above results in HTML output of

<li><a href="">name of sub-cat</a></li>`

Does anyone know how I can get the URL for a category page from a catalog/category object?

Answer

user487772 picture user487772 · May 5, 2011

Replace

$children = Mage::getModel('catalog/category')->getCategories(3);

with

$children = Mage::getModel('catalog/category')->load(3)->getChildrenCategories();