LucyとRuby

【WordPress】get_terms()を使用するテンプレート(コピペ用)

WordPressのget_terms()関数はパラメータ数が多くて使用時によく迷います。

そこで簡単に使えるようにコピペで使えるテンプレートにしました。

タクソノミー、タームを簡単に説明

WordPressで最もイメージがわかなくてとっつきにくい単語ワン・ツーが「タクソノミー」と「ターム」ではないでしょうか。

「タクソノミー」は投稿記事を整理する「分類方法」で、「ターム」は具体的な「分類名」です。例を挙げると「カテゴリー」という「分類方法」があって、具体的な「分類名」が「和食、洋食、中華」です。

WordPressではデフォルトのタクソノミーとして「カテゴリ(category)」と「タグ(post_tag)」があります。タクソノミーとタームを整理すると下の表のようになります。

タクソノミー(分類方法) ターム(分類名)
category 和食, 洋食, 中華
post_tag オススメ, 人気店, 要予約

上の表のように、タクソノミーとタグは実は簡単なものです。恐れるに足らず。タクソノミーは自分で追加することもできます。

get_terms()を使用するテンプレート

get_temrs()の全てのパラメータと戻り値のコードサンプルです。代入しているパラメータは全てデフォルト値(省略可)です。パラメータを変えたり、不要な箇所を削除したりして使います。

<?php
$taxonomy = 'post_tag';    // 取得するタクソノミーを決める('category'や'post_tag')
//get_terms()でタームの情報を取得
$terms = get_terms(array(
  'taxonomy'               => $taxonomy, // string|string[] 取得するタクソノミー('category'や'post_tag')
  'object_ids'             => null,      // int|int[] 取得する投稿IDを指定する(ループ内ならget_the_ID()で指定可)
  'orderby'                => 'name',    // string ソート順のキーとするフィールド名('name', 'slug', 'term_group', 'term_id', 'id', 'description', 'parent', 'term_order')
  'order'                  => 'ASC',     // string ソート順 昇順('ASC')または降順('DESC')
  'hide_empty'             => true,      // bool|int 投稿がない場合は取得しないかどうか 取得しない(true)、取得する(false)
  'include'                => array(),   // int[]|string 取得するタームID 配列またはカンマ区切りで指定
  'exclude'                => array(),   // int[]|string 除外するタームID 配列またはカンマ区切りで指定
  'exclude_tree'           => array(),   // int[]|string 子孫タームを含めて除外するタームID 配列またはカンマ区切りで指定
  'number'                 => '',        // int|string 取得する最大件数
  'offset'                 => '',        // int 取得開始行
  'fields'                 => 'all',     // string 取得するタームフィールド('all' , 'names' , 'slugs' ,  'count' など)
  'count'                  => false,     // bool タームカウント数を返すか【使い方不明】
  'name'                   => '',        //string|string[] 取得するターム名
  'slug'                   => '',        // string|string[] 取得するスラッグ
  'term_taxonomy_id'       => '',        //int|int[] タームタクソノミーID【使い方不明】
  'hierarchical'           => true,      // bool 子孫タームが空でない場合は、hide_emptyがtrueであっても取得する
  'search'                 => '',        // string タームの文字列検索【使い方不明】
  'name__like'             => '',        // string タームの名前の文字列検索
  'description__like'      => '',        // string タームの説明の文字列検索
  'pad_counts'             => false,     // bool countに子供の数を含めるかどうか
  'get'                    => '',        // string 先祖またはタームが空であっても取得するか( 'all' または '' )
  'child_of'               => 0,         // int 取得する子タームの親タームID
  'parent'                 => '',        // int 取得する親タームID
  'childless'              => false,     // bool 子タームの無いタームに限定するか 限定する(true)、限定しない(false)
  'cache_domain'           => 'core',    // string Unique cahe key to produced【使い方不明】
  'cache_results'          => true,      // bool Whether to cache term information【使い方不明】
  'update_term_meta_cache' => true,      // bool Whether to prime meta caches【使い方不明】
  'meta_key'               => '',        // string|string[] 【MySQL関連】【使い方不明】
  'meta_value'             => '',        // string|string[] 【MySQL関連】【使い方不明】
  'meta_compare'           => '',        // string 【MySQL関連】【使い方不明】
  'meta_compare_key'       => '',        // string 【MySQL関連】【使い方不明】
  'meta_type'              => '',        // string 【MySQL関連】【使い方不明】
  'meta_type_key'          => '',        // string【MySQL関連】【使い方不明】
  'meta_query'             => '',        // string【使い方不明】
));
//取得したタームを展開
foreach($terms as $term){
  $term_id               = $term->term_id;              //タームID
  $term_name             = $term->name;                 //ターム名
  $term_slug             = $term->slug;                 //スラッグ
  $term_description      = $term->description;          //タームの説明
  $term_count            = $term->count;                //タームの投稿数
  $term_parent           = $term->parent;               //親タームのID
  $term_term_group       = $term->term_group;           //タームのグループ
  $term_term_taxonomy_id = $term->term_taxonomy_id;     //タームのタクソノミーID
  $term_taxonomy         = $term->taxonomy;             //タクソノミー名
  $term_link             = get_term_link($term_id, $taxonomy); //タームのURL

  //確認ため値をhtmlに書き出し(参考)
  echo '$term_id               = ' . $term_id               . '<br>' . PHP_EOL; //タームID
  echo '$term_name             = ' . $term_name             . '<br>' . PHP_EOL; //ターム名
  echo '$term_slug             = ' . $term_slug             . '<br>' . PHP_EOL; //スラッグ
  echo '$term_description      = ' . $term_description      . '<br>' . PHP_EOL; //タームの説明
  echo '$term_count            = ' . $term_count            . '<br>' . PHP_EOL; //タームの投稿数
  echo '$term_parent           = ' . $term_parent           . '<br>' . PHP_EOL; //親タームのID
  echo '$term_term_group       = ' . $term_term_group       . '<br>' . PHP_EOL; //タームのグループ
  echo '$term_term_taxonomy_id = ' . $term_term_taxonomy_id . '<br>' . PHP_EOL; //タームのタクソノミーID
  echo '$term_taxonomy         = ' . $term_taxonomy         . '<br>' . PHP_EOL; //タクソノミー名(category)
  echo '$term_link             = ' . $term_link             . '<br>' . PHP_EOL; //タームのURL
}

以上です。

post no. 718

Comments