Joomla Get subcategories of custom parent category

6.3k Views Asked by At

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.

2

There are 2 best solutions below

2
On BEST ANSWER

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

0
On

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();
}