WordPress Snippets at WPcustoms

Conditional tag – category has children

We build our own conditional tag with this function to see if our current category does have children categories or not. Do whatever you require in each case – useful to show the children categories or link to other related stuff,


/**
 * Snippet Name: Conditional tag – category has children
 * Snippet URL: https://wpcustoms.net/snippets/conditional-tag-for-children-categories/
 */
  function category_has_children() {
global $wpdb;
$term = get_queried_object();
$category_children_check = $wpdb->get_results(" SELECT * FROM wp_term_taxonomy WHERE parent = '$term->term_id' ");
    if ($category_children_check) {
        return true;
    } else {
        return false;
    }
}


if (!category_has_children()) {
    echo 'this category does not have child categories.';

} else {
echo 'Houston, we have child categories :)';
}