基本上,您只需检查$links 变量不为空,如果为空,则回显文本Tag: 以及$links (即术语表):
if ( ! empty( $links ) ) {
echo \'Tags: \' . implode( \', \', $links );
}
或更好的版本,请检查
$terms 不是
WP_Error 实例和
$terms 不为空:
$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 );
}
但是,如果您只需要显示术语(在
custom taxonomy this_is_custom) 形式为
Tags: <tag with link>, <tag with link>, ..., i、 e.简单的可点击链接,然后您可以简单地使用
the_terms():
// Replace your entire code with just:
the_terms( $post->ID, \'this_is_custom\', \'Tags: \', \', \' ); // but I would use \', \' instead of \', \'