在WordPress主题中,如何列出与帖子和某个分类相关联的所有术语,并且仅显示属于设定术语的子项的术语。并设置自定义分隔符?
答案 0 :(得分:0)
将它放在你的functions.php中:
/*
* Get Terms as a list
*/
function get_term_list_not_linked($postID, $tax, $parentTermID, $sep) {
$terms = get_the_terms( $postID, $taxonomy);
if ( $terms && ! is_wp_error( $terms ) ) {
$termList = array();
foreach ( $terms as $term ) {
if($term->parent == $parentTermID) {
$term_list[] = $term->name;
}
}
$termList = join( $separator, $termList);
return $termList;
} else {
return null;
}
}
然后在主题文件中,你这样称呼它:
<?php echo get_term_list_not_linked($postID, $taxonomy, $parentTermID, $separator); ?>
以下示例获取“视频类型”分类中的所有术语,即ID为5的术语的直接子项,并使用逗号和空格分隔术语:
<?php echo get_term_list_not_linked($post->ID, 'video-type', 5, ', '); ?>