Skip to main content
added 198 characters in body
Source Link
Howdy_McGee
  • 20.8k
  • 24
  • 91
  • 175

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 ) )

You could use this simple function call which returns either TRUE or FALSE depending on if $children is an empty array or not.

function category_has_children( $term_id = 0, $taxonomy = 'category' ) {
    $children = get_categories( array( 'child_of' => $term_id, 'taxonomy' => $taxonomy ) );
    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 ) )

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 ) )
Updated answer based on comments
Source Link
Howdy_McGee
  • 20.8k
  • 24
  • 91
  • 175

You could use this simple function call which returns either TRUE or FALSE depending on if $children is an empty array or not.

function category_has_children( $term_id = 0, $post_type = 'post', $taxonomy = 'category' ) {
    $children = get_categories( array( 'child_of' => $term_id, 'type' => $post_type, 'taxonomy' => $taxonomy ) );
    return ( $children );
}

So if you're only using the built-in post categories you can call itthe function like so: category_has_children( 17 );

If you're usingyou need to test a custom post type and taxonomy it will still work almost the same, you'll just need to pass in a few more parametersan extra parameter 'taxonomy-slug': category_has_children( 7, 'my_post_type', 'my_taxonomy' );'

To call it in your IF statement:

if( $cat->parent != 0 && ! category_has_children( $cat->term_id ) )

You could use this simple function call which returns either TRUE or FALSE depending on if $children is an empty array or not.

function category_has_children( $term_id = 0, $post_type = 'post', $taxonomy = 'category' ) {
    $children = get_categories( array( 'child_of' => $term_id, 'type' => $post_type, 'taxonomy' => $taxonomy ) );
    return ( $children );
}

So if you're only using the built-in post you can call it like so: category_has_children( 17 );

If you're using a custom post type and taxonomy it will still work the same, just need to pass in a few more parameters: category_has_children( 7, 'my_post_type', 'my_taxonomy' );'

To call it in your IF statement:

if( $cat->parent != 0 && ! category_has_children( $cat->term_id ) )

You could use this simple function call which returns either TRUE or FALSE depending on if $children is an empty array or not.

function category_has_children( $term_id = 0, $taxonomy = 'category' ) {
    $children = get_categories( array( 'child_of' => $term_id, 'taxonomy' => $taxonomy ) );
    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 ) )
Source Link
Howdy_McGee
  • 20.8k
  • 24
  • 91
  • 175

You could use this simple function call which returns either TRUE or FALSE depending on if $children is an empty array or not.

function category_has_children( $term_id = 0, $post_type = 'post', $taxonomy = 'category' ) {
    $children = get_categories( array( 'child_of' => $term_id, 'type' => $post_type, 'taxonomy' => $taxonomy ) );
    return ( $children );
}

So if you're only using the built-in post you can call it like so: category_has_children( 17 );

If you're using a custom post type and taxonomy it will still work the same, just need to pass in a few more parameters: category_has_children( 7, 'my_post_type', 'my_taxonomy' );'

To call it in your IF statement:

if( $cat->parent != 0 && ! category_has_children( $cat->term_id ) )