Yes, get_category_children()
is indeed deprecated and you should use get_term_children()
instead. So with your original code:
// You can simply replace this:
if (get_category_children($this_category->cat_ID) != "")
// with this:
$ids = get_term_children( $this_category->cat_ID, 'category' );
if ( ! empty( $ids ) )
And with your second code which uses get_terms()
, you can check if the terms list is not empty and if it’s not, display the terms; else, display the posts:
$terms = get_terms( ... );
if ( ! empty( $terms ) ) {
// Display the terms.
echo '<ul class="subsubcats"><li class="categories"><ul>';
foreach ( $terms as $term ) {
... your code here ...
}
echo '</ul></li></ul>';
} else {
// Display the posts.
get_template_part( 'loop' );
}
But then, for what you’re trying to do, it can actually be achieved with just wp_list_categories()
:
$list = wp_list_categories( array(
'orderby' => 'title',
'title_li' => '',
'child_of' => get_queried_object_id(), // just change the value if necessary
'show_option_none' => '', // this does the trick
'echo' => 0,
) );
if ( ! empty( $list ) ) {
echo '<ul class="subsubcats">' . $list . '</ul>';
} else {
get_template_part( 'loop' );
}