get_the_terms
returns an Array of WP_Term objects on success, false if there are no terms or the post does not exist, WP_Error on failure.
You can always check if your $terms
is an array or is_wp_error
.
To check if $terms
is an array:
<?php $terms = get_the_terms( $post->ID , 'tvshows_categories' );
if ( is_array( $terms ) && ! is_wp_error( $terms ) ) {
foreach ($terms as $term) {
$term_link = get_term_link($term, 'tvshows_categories');
if (is_wp_error($term_link))
continue;
echo '<a href="' . $term_link . '">' . $term->name . '</a>, ';
}
}
?>
Or if you’re on a single post, you can always check if you’re on your custom post type.
To check if you’re on a custom post type on a single page:
<?php
if ( is_singular('YOUR_CUSTOM_POST_TYPE') ) {
$terms = get_the_terms($post->ID, 'tvshows_categories');
foreach ($terms as $term) {
$term_link = get_term_link($term, 'tvshows_categories');
if (is_wp_error($term_link))
continue;
echo '<a href="' . $term_link . '">' . $term->name . '</a>, ';
}
}
?>