How do we count the number of child categories of a parent category? I am using a custom taxonomy, called: “authors”.
I found this code, which does count the number of child categories, but only globally, not of one specific parent.
$num_cats = wp_count_terms('authors');
$num_parent_cats=count(get_categories('parent=0&hide_empty=0'));
$num_child_cats = $num_cats-$num_parent_cats;
echo '<p>number of child categories: ' . $num_child_cats . '</p>';
Source: https://wordpress.org/support/topic/child-category-count
1 Answer
Use get_term_children()
$term_id = 2; // use get_queried_object()->term_id; to get the current term id
$taxonomy_name="mypages"; // use use get_queried_object()->taxonomy; to get the current taxonomy name
$countchildren = count (get_term_children( $term_id, $taxonomy_name ));
echo $countchildren;