Wordpress - Check if Current Category has Children
There may or may not be a better way to do this, but here's how I would do it:
$term = get_queried_object();
$children = get_terms( $term->taxonomy, array(
'parent' => $term->term_id,
'hide_empty' => false
) );
// print_r($children); // uncomment to examine for debugging
if($children) { // get_terms will return false if tax does not exist or term wasn't found.
// term has children
}
If current taxonomy term has children the get_terms
function will return an array, otherwise it will return false
.
Tested and works on my local vanilla install with Custom Post Type UI plugin used for CPT generation.
There's also a generic WP possibility to do this via get_term_children.
<?php
$children = get_term_children($termId, $taxonomyName);
if( empty( $children ) ) {
//do something here
}