get_the_terms
成功时返回WP\\u Term对象数组,如果没有术语或post不存在,则返回false,失败时返回WP\\u Error。
您可以随时检查$terms
是数组或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>, \';
}
}
?>
或者,如果您在一篇文章中,您可以随时检查您是否在自定义文章类型中。
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>, \';
}
}
?>