Display taxonomy term only if there’s a value

get_terms() return all the terms in the taxonomy regardless of the current post.
So if current post does not have lang terms still this condition if ( !empty( $lang)) will be true thus it will display the text Language.

Use get_the_term_list instead of the_terms which return the terms list instead of printing, so output can be used for both purpose.

Example:-

$lang = get_the_term_list( get_the_ID(), 'lang', ' ', ', ' );
if ( !empty( $lang)) {
    echo '<li><strong>Language:</strong>' . $lang . '</li>';
}

Note: Recommended approach is to use get_the_ID() instead of global
$post->ID

Leave a Comment