この記事は2年以上前に書かれたものです。
情報が古い可能性があります。
情報が古い可能性があります。
これを覚えておくとタームのWP_Queryなどをサブループとして使って、最新記事情報を取得、なんてことも可能だと思います。
検証バージョン
- WordPress 3.8.1
カスタムタクソノミー (カスタム分類) のターム一覧を表示する
例1 (index.php)
<?php
// カスタム分類名
$taxonomy = 'food';
// パラメータ
$args = array(
// 子タームの投稿数を親タームに含める
'pad_counts' => true,
// 投稿記事がないタームも取得
'hide_empty' => false
);
// カスタム分類のタームのリストを取得
$terms = get_terms( $taxonomy , $args );
if ( count( $terms ) != 0 ) {
echo '<ul>';
// タームのリスト $terms を $term に格納してループ
foreach ( $terms as $term ) {
// タームのURLを取得
$term = sanitize_term( $term, $taxonomy );
$term_link = get_term_link( $term, $taxonomy );
if ( is_wp_error( $term_link ) ) {
continue;
}
// 子タームの場合はCSSクラス付与
if( $term->parent != 0 ) {
echo '<li class="children">';
} else {
echo '<li>';
}
// タームのURLと名称を出力
echo '<a href="' . esc_url( $term_link ) . '">' . $term->name . '</a>(' . $term->count . ')';
echo '</li>';
}
echo '</ul>';
}
?>
例2 (index.php)
<?php
// カスタム分類名
$taxonomy = 'food';
// パラメータ
$args = array(
// 親タームのみ取得
'parent' => 0,
// 子タームの投稿数を親タームに含める
'pad_counts' => true,
// 投稿記事がないタームも取得
'hide_empty' => false
);
// カスタム分類のタームのリストを取得
$terms = get_terms( $taxonomy , $args );
if ( count( $terms ) != 0 ) {
echo '<div>';
// 親タームのリスト $terms を $term に格納してループ
foreach ( $terms as $term ) {
// 親タームのURLを取得
$term = sanitize_term( $term, $taxonomy );
$term_link = get_term_link( $term, $taxonomy );
if ( is_wp_error( $term_link ) ) {
continue;
}
// 親タームのURLと名称とカウントを出力
echo '<h3><a href="' . esc_url( $term_link ) . '">' . $term->name . '</a>(' . $term->count . ')</h3>';
// 子タームのIDのリストを取得
$term_children = get_term_children( $term->term_id, $taxonomy );
if( count( $term_children ) != 0 ) {
echo '<ul>';
// 子タームのIDのリスト $term_children を $term_idに格納してループ
foreach ( $term_children as $term_id ) {
// 子タームのIDを元に子タームの情報を取得
$term_child = get_term_by( 'id', $term_id, $taxonomy );
// 子タームのURLを取得
$term_child = sanitize_term( $term_child, $taxonomy );
$term_child_link = get_term_link( $term_child, $taxonomy );
if ( is_wp_error( $term_child_link ) ) {
continue;
}
// 子タームのURLと名称とカウントを出力
echo '<li><a href="' . esc_url( $term_child_link ) . '">' . $term_child->name . '</a>(' . $term_child->count . ')</li>';
}
echo '</ul>';
}
}
echo '</div>';
}
?>
実行例 (例1と例2を同時に実行)

個別投稿ページでカスタムタクソノミー (カスタム分類) のターム一覧を表示し、投稿で選択したタームにCSSのクラスを付与する
single.php
<?php while ( have_posts() ) : the_post(); ?>
<?php
// カスタム分類名
$taxonomy = 'food';
// パラメータ
$args = array(
// 投稿記事がないタームも取得
'hide_empty' => false
);
// カスタム分類のタームのリストを取得
$terms = get_terms( $taxonomy , $args );
if ( count( $terms ) != 0 ) {
echo '<ul>';
// タームのリスト $terms を $term に格納してループ
foreach ( $terms as $term ) {
// 投稿でタームのスラッグを選択していれば、current-catを付与
if ( is_object_in_term( get_the_ID(), $taxonomy, $term->slug ) ) {
echo '<li class="current-cat">';
} else {
echo '<li>';
}
// タームの名称を出力
echo esc_html( $term->name );
echo '</li>';
}
echo '</ul>';
}
?>
<?php endwhile; ?>
HTML出力例
<ul>
<li class="current-cat">ご飯もの</li>
<li class="current-cat">チャーハン</li>
<li>バナナ</li>
<li class="current-cat">リンゴ</li>
<li>果物</li>
</ul>
[…] http://webcake.stars.ne.jp/webdesign/get-terms.html […]
こんにちわ!非常に助かりました。
質問なのですが、すべてのタームではなく、指定のターム&子タームのみ取得することは可能でしょうか?
関数リファレンス/get term
http://wpdocs.osdn.jp/%E9%96%A2%E6%95%B0%E3%83%AA%E3%83%95%E3%82%A1%E3%83%AC%E3%83%B3%E3%82%B9/get_term
関数リファレンス/get terms
http://wpdocs.osdn.jp/%E9%96%A2%E6%95%B0%E3%83%AA%E3%83%95%E3%82%A1%E3%83%AC%E3%83%B3%E3%82%B9/get_terms
このあたりでしょうか。get_termsに ‘include’ というパラメータがあるので、タームのIDを指定するという手もあります。