情報を追記している場合はありますが、古い情報を訂正はしていませんので、公開年月日を参照してください。プラグイン・タグ、いずれもワードプレス・PHPのバージョン等によって動作しない場合もあります。
ループ内で、記事が属しているタームとタームの説明をリンク付きで表示。*タームの日本語表記と英語表記を2行で表示したいという要望のため
<?php
$terms = wp_get_object_terms( $post->ID, 'event_area' );
if ( $terms && !is_wp_error( $terms ) ) {
echo '<ul>';
foreach ( $terms as $term ) {
echo '<li><a href="' . get_term_link( $term, $term->taxonomy ) . '">' . $term->name . '<br />' . $term->description . '</a></li>';
}
echo '</ul>';
}
?>
タームの選択が複数で、階層順に表示したい場合。wp_get_object_terms の場合はarray(‘orderby’ => ‘term_order’)、http://codex.wordpress.org/Function_Reference/wp_get_object_terms 参照
get_the_term_list の場合は以下をfunctions に記入。参照元http://wordpress.stackexchange.com/questions/7440/change-order-of-custom-taxonomy-list
function set_the_terms_in_order ( $terms, $id, $taxonomy ) {
$terms = wp_cache_get( $id, "{$taxonomy}_relationships_sorted" );
if ( false === $terms ) {
$terms = wp_get_object_terms( $id, $taxonomy, array( 'orderby' => 'term_order' ) );
wp_cache_add($id, $terms, $taxonomy . '_relationships_sorted');
}
return $terms;
}
add_filter( 'get_the_terms', 'set_the_terms_in_order' , 10, 4 );
function do_the_terms_in_order () {
global $wp_taxonomies;
$wp_taxonomies['post_tag']->sort = true;
$wp_taxonomies['post_tag']->args = array( 'orderby' => 'term_order' );
}
add_action( 'init', 'do_the_terms_in_order');
カスタム投稿AとBそれぞれにカスタム分類「エリア」を作成し、Aの個別ページでBの同じエリアの記事を表示
<?php $terms = wp_get_object_terms($post->ID,'A_area'); foreach ( $terms as $term ) { $area = $term->slug; }
$query = new WP_Query(array(
'post_type' => 'B',
'B_area' => $area,
'posts_per_page' => '1',
));
?>
タームの選択が複数の場合
$terms = get_the_terms( $post->ID, 'A_area' );
if ( $terms && ! is_wp_error( $terms ) ) {
$area = array();
foreach ( $terms as $term ) {
$area[] = '"' . $term->slug . '"';
}
$areas = join( ',', $area );
}
?>
シングルページでタームリストをリンクなし・セパレーター付きで
<?php
if ($terms = get_the_terms($post->ID, 'タクソノミー名')){
$termnames = array();
foreach( $terms as $term ){
array_push( $termnames, $term->name );
}
echo join(', ',$termnames);
} ?>