I am not aware of a built in function that does what you are asking but it is not that hard to cook up. In fact, you are pretty close. get_categories
is correct but it needs the child_of
argument, which means finding the topmost parent via get_ancestors
. child_of
will only return children, not the specified parent, so that parent has to be inserted into the results manually.
var_dump(get_category_parents(5)); // reference
$anc = get_ancestors(5,'category');
$parent = array_pop($anc);
$hier[] = get_category($parent);
$args = array(
'child_of' => $parent,
);
array_push($hier,get_categories($args));
var_dump($hier);