WordPress テーマで、投稿に関連付けられたすべての用語と特定の分類法をどのように一覧表示し、設定された用語の子である用語のみを表示するのですか? カスタムセパレーターを設定しますか?
1 に答える
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 の用語の直接の子である「video-type」分類法のすべての用語を取得し、用語をカンマとスペースで区切る例を次に示します。
<?php echo get_term_list_not_linked($post->ID, 'video-type', 5, ', '); ?>
于 2012-08-11T06:02:43.080 に答える