I am trying to display a list of all categories and sub-categories underneath that category, I have managed to get a list of all categories that are parents but am struggling to detect if it has children and then display them in the current loop. Here is my code at the moment:
$args = array(
'orderby' => 'name',
'parent' => 0
);
$categories = get_categories( $args );
foreach ( $categories as $category ) {
echo '<a href="' . get_category_link( $category->term_id ) . '">' . $category->name . '</a><br />';
}
I have found a few functions on here about detecting if the category has children, but am struggling to actually display the children (up to one level) under this link.
Any help is greatly appreciated,
Simon
I have very 4 lines solutions, It shows category and sub-categories and their posts count.
Solved with the following:
If you want to display all your categories and subcategories of custom taxonomy, use this code and make sure to provide the slug of your taxonomy.