うさぎのイラスト

ネットアンサー55備忘録

web技術を書いていきます

タクソノミーが持っているターム全部表示

-2015年05月26日-
[◎1]

illustrator_catの部分を表示させたいタクソノミーを書き込むだけ。
1<?php wp_list_categories(array('title_li' => '', 'taxonomy' => 'illustrator_cat', 'show_count' => 1)); ?>   


★タクソノミー名を振り分けるときはこれを使う
1<?php $cat=(get_post_type_object(get_post_type())->name).'_cat'; ?>
2<?php wp_list_categories(array('title_li' => '', 'taxonomy' => $cat, 'show_count' => 1)); ?>


[◎2]

illustrator_catの部分を表示させたいタクソノミーを書き込むだけ。
1                <?php
2// カスタム分類名
3$taxonomy = 'illustrator_cat';
4 
5// パラメータ
6$args = array(
7    // 子タームの投稿数を親タームに含める
8    'pad_counts' => true,
9   
10    // 投稿記事がないタームも取得
11    'hide_empty' => false
12);
13 
14// カスタム分類のタームのリストを取得
15$terms = get_terms( $taxonomy , $args );
16 
17if ( count( $terms ) != 0 ) {
18    echo '<ul>';
19      
20    // タームのリスト $terms を $term に格納してループ
21    foreach ( $terms as $term ) {
22     
23        // タームのURLを取得
24        $term = sanitize_term( $term, $taxonomy );
25        $term_link = get_term_link( $term, $taxonomy );
26        if ( is_wp_error( $term_link ) ) {
27            continue;
28        }
29         
30        // 子タームの場合はCSSクラス付与
31        if( $term->parent != 0 ) {
32            echo '<li class="children">';
33        } else {
34            echo '<li>';
35        }
36         
37        // タームのURLと名称を出力
38        echo '<a href="' . esc_url( $term_link ) . '">' . $term->name . '</a>(' . $term->count . ')';
39        echo '</li>';
40    }
41    
42echo '</ul>';
43}
44?>