Question

I have a category ID and wan't to display all the subcategories. Show should I do that in Joomla?

I've tried the following

$catID = JRequest::getVar('id');
$categories = JCategories::getInstance('Content');
$cat = $categories->get($catID);
$children = JCategoryNode::getChildren($cat);
printObject($children);

But it doesn't work.

Was it helpful?

Solution

getChildren is not a static function, you call it off the category object that you get from get, which should be of type JCategoryNode.

$catID = JRequest::getVar('id');
$categories = JCategories::getInstance('Content');
$cat = $categories->get($catID);
$children = $cat->getChildren();
print_r($children);

JCategorNode api

OTHER TIPS

As of Joomla! 3.9 as well as Joomla! 4 you should use something like this:

private static function getCatChildren($id)
{
    $categories = \Joomla\CMS\Categories\Categories::getInstance('component_name');
    $cat        = $categories->get($id);
    return  $cat->getChildren();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top