You could use this simple function call which returns either TRUE
or FALSE
depending on if $children
is an empty array or not.
/**
* Check if given term has child terms
*
* @param Integer $term_id
* @param String $taxonomy
*
* @return Boolean
*/
function category_has_children( $term_id = 0, $taxonomy = 'category' ) {
$children = get_categories( array(
'child_of' => $term_id,
'taxonomy' => $taxonomy,
'hide_empty' => false,
'fields' => 'ids',
) );
return ( $children );
}
So if you're only using the built-in post
categories you can call the function like so: category_has_children( 17 );
If you need to test a custom taxonomy it will work almost the same, you'll just need to pass in an extra parameter 'taxonomy-slug': category_has_children( 7, 'my_taxonomy' );
'
To call it in your IF statement:
if( $cat->parent != 0 && ! category_has_children( $cat->term_id ) )