Basically, you just need to check if the $links
variable is not empty and if so, then echo the text Tag:
and the $links
(i.e. the terms list):
if ( ! empty( $links ) ) {
echo 'Tags: ' . implode( ', ', $links );
}
Or a better version, check if the $terms
is not a WP_Error
instance and $terms
is not empty:
$terms = get_the_terms( $post->ID , 'this_is_custom' );
if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
$links = [];
foreach ( $terms as $term ) {
// ... your code.
}
echo 'Tags: ' . implode( ', ', $links );
}
However, if all you need is to display the terms (in the custom taxonomy this_is_custom
) in the form of Tags: <tag with link>, <tag with link>, ...
, i.e. simple clickable links, then you could simply use the_terms()
:
// Replace your entire code with just:
the_terms( $post->ID, 'this_is_custom', 'Tags: ', ', ' ); // but I would use ', ' instead of ', '