0

I want to show term child only if has a post here is my code, can you please reply me where i am doing wrong.

<li><a href="#" class="dropdown-toggle" data-toggle="dropdown">DESTINOS <i class="fa fa-angle-down"></i></a>
<?php
$term_id =169;
$taxonomy_name = 'spanishcategory';
$term_children = get_term_children( $term_id, $taxonomy_name );

echo '<ul class="dropdown-menu">';
foreach ( $term_children as $child ) {
    $term = get_term_by( 'id', $child, $taxonomy_name );
    echo '<li><a href="' . get_term_link( $child, $taxonomy_name ) . '">' . $term->name . '</a></li>';
}
?>

1 Answer 1

0

AS per my knowledge you need to write following function in your functions.php and check whether the term has post using the below function

function check_term_posts($tax_slug, $term_id) {
    $args = array(
        'post_type' => 'post',
        'status' => 'publish',
        'tax_query' => array(
            array(
                'taxonomy' => spanishcategory,
                'field' => 'term_id',
                'terms' => 169
            )
        )
    );
    $term_query =  new WP_Query($args);
    $term_posts_count = $term_query->found_posts;
    if( $term_posts_count>0 ){
        return true;// You can code here to echo name of category
    } else {
        return false;
    }
}

Do let me know if this can solve your error or need any help?

3
  • Hi Pratik, Thanks for answer, can tell me please the above my will be same or not ? I added your code in my function.php but it's not giving me only child if has a post.
    – Shiva
    Commented Sep 21, 2017 at 7:24
  • Are you using the function where you need to display the term? Commented Sep 21, 2017 at 7:27
  • I got this error after that : Warning: Missing argument 1 for check_term_posts(), called in /var/sites/s/stagingblog.isango.com/public_html/wp-content/themes/canada/Spanish.php on line 84 and defined in /var/sites/s/stagingblog.isango.com/public_html/wp-content/themes/canada/functions.php on line 727
    – Shiva
    Commented Sep 21, 2017 at 8:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.