0

I have three custom taxonomies (topic, name, simile) Currently I display the list of tags in each taxonomy for the current post at the bottom of the post. That works fine. But I would like to give each taxonomy a heading, but only if there is something displayed for that taxonomy.

I am currently doing a similar thing with custom fields. For example the further-reading field:

<?php 
    $my_furtherReading = get_post_meta( get_the_ID(), 'further-reading', true);

    if( ! empty( $my_furtherReading ) ) {
        echo '<div class="furtherReading"><h2>Further Reading:</h2>' . $my_furtherReading . '</div>';
    }
?>  

It only prints the heading if the field is not empty.

This is how I am displaying my taxonomies (topic taxonomy)

<?php
echo get_the_term_list( $post->ID, 'topic',
'', ' ', '' );
?>

Is there a function to test if the tag list is empty? If so, could someone be kind enough to show me how to use it?

1

1 Answer 1

0

You can use wp_get_post_terms() function, like this:

if ( wp_get_post_terms( $post->ID, 'topic', [ 'fields' => 'ids' ] ) ) {
    // Your code
}

We passed the 'fields' => 'ids' in args, just to prevent instantiation of the full WP_Term objects in the background, since you only need to check if there are any, and using a different function for the HTML generation.

1

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.