【WordPress】カスタム投稿タイプのタクソノミーやターム関連のタグと関数を11個をまとめました
※当ブログのリンクには一部広告が含まれています。
WordPressのカスタム投稿タイプやカスタム分類はとても便利ですが、テンプレートやテンプレートタグが多くて、どれを使ったら何が取得・表示されるのか分からなくなりますよね。
毎回カスタム投稿を触る度に調べるのが面倒なので、カスタム投稿、カスタム分類(タクソノミー、ターム)に関連するタグや関数をまとめてみました!
ここに載せている情報の殆どは、WordPress Codex 日本語版を参考にさせていただいています。(文章やコードサンプルも少しお借りしています)パラメーターは必須のものとよく使いそうなオプションのみ載せており、戻り値の出力結果はvar_dumpで出力しています。また、コードサンプルは以下の状況を想定したものです。
- カスタム投稿タイプ
- 観光情報(travel)
- タクソノミー
- 地域(area)階層ありカテゴリー
- ターム
- 東京(tokyo)-代官山(daikanyama)…、埼玉(saitama)-大宮(omiya)…
目次 [非表示]
get_terms
指定したタクソノミーに含まれるターム情報を取得し、オブジェクトの配列で返します。もしタクソノミーなどが存在しなければWP_Error オブジェクトを返します。配列で返ってくるので、foreachなどで繰り返し処理して出力します。
1 | <?php $terms = get_terms( $taxonomies , $args ); ?> |
関数リファレンス/get terms – WordPress Codex 日本語版
パラメーター
- $taxonomies
- (文字列|配列)(必須):タームを取得するタクソノミー。
- $args
- (文字列|配列)(オプション) 戻り値の種類を変更。指定できる引数はこちら。
戻り値
以下はareaタクソノミーで記事があるターム全情報の出力結果例です。タームが10個あれば、array(10)で10個分出力されます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | array (2) { [0]=> object(stdClass)#xxxx (9) { [ "term_id" ]=> string(2) "46" [ "name" ]=> string(10) "東京" [ "slug" ]=> string(4) "tokyo" [ "term_group" ]=> string(1) "0" [ "term_taxonomy_id" ]=> string(2) "46" [ "taxonomy" ]=> string(7) "area" [ "description" ]=> string(0) "" [ "parent" ]=> string(1) "0" [ "count" ]=> string(1) "0" } [1]=> object(stdClass)##xxxx (9) { [ "term_id" ]=> string(2) "48" [ "name" ]=> string(7) "表参道" [ "slug" ]=> string(4) "omotesando" [ "term_group" ]=> string(1) "0" [ "term_taxonomy_id" ]=> string(2) "48" [ "taxonomy" ]=> string(7) "area" [ "description" ]=> string(0) "" [ "parent" ]=> string(2) "46" //親カテゴリーがある場合は、親のIDを返す。なければ0を返す。 [ "count" ]=> string(1) "1" } } |
サンプルコード
1 2 3 4 5 6 7 8 | $terms = get_terms( 'my_taxonomy' ); if ( ! empty ( $terms ) && !is_wp_error( $terms ) ){ echo '<ul>' ; foreach ( $terms as $term ) { echo '<li>' . $term ->name . '</li>' ; } echo '</ul>' ; } |
get_term
タームIDを指定してタームの全データをデータベースから取得します。タームIDは指定したタクソノミーに含まれるタームでなければ取得できません。
1 | <?php get_term( $term , $taxonomy , $output , $filter ) ?> |
パラメーター
- $term
- (必須):整数なら、それを ID とするタームをデータベースから取得する。
- taxonomy
- (必須):$termが含まれるタクソノミーの名前。タクソノミーが存在しなければWP_Errorが返される。
戻り値
1 2 3 4 5 6 7 8 9 10 11 12 | object(stdClass)#xxxx (10) { [ "term_id" ]=> int(12) [ "name" ]=> string(9) "代官山" [ "slug" ]=> string(9) "daikanyama" [ "term_group" ]=> int(0) [ "term_taxonomy_id" ]=> int(12) [ "taxonomy" ]=> string(9) "area" [ "description" ]=> string(0) "" [ "parent" ]=> int(0) [ "count" ]=> int(2) [ "filter" ]=> string(3) "raw" } |
サンプルコード
1 2 3 4 5 6 7 8 9 10 11 | //指定された内容のターム情報を取得する $term = get_term( $term_id , $taxonomy ); //スラッグ名を取得する $slug = $term ->slug; //ターム名を取得する $name = $term ->name; //タームのディスクリプションを取得する $desc = $term ->description; |
get_the_terms
投稿IDから投稿記事が属するタクソノミー情報を取得します。こちらも配列で返ってくるので、foreachなどで出力します。
1 | <?php get_the_terms( $id , $taxonomy ); ?> |
パラメーター
- $id
- (整数)(必須) 投稿ID。現在表示しているページなら$post->IDで取得可能。
- $taxonomy
- (文字列)(必須)タームを取得するタクソノミーの名前。
戻り値:3つのタームを持つ投稿の場合
値がなければbool(false)が返されます
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | array (3) { [0]=> object(stdClass)#xxxx (11) { [ "term_id" ]=> int(7) [ "name" ]=> string(9) "代官山" [ "slug" ]=> string(9) "daikanyama" [ "term_group" ]=> int(0) [ "term_taxonomy_id" ]=> int(7) [ "taxonomy" ]=> string(9) "area" [ "description" ]=> string(0) "" [ "parent" ]=> int(0) [ "count" ]=> int(4) [ "object_id" ]=> int(70) [ "filter" ]=> string(3) "raw" } [1]=> object(stdClass)#xxxx (11) { [ "term_id" ]=> int(14) [ "name" ]=> string(9) "大宮" [ "slug" ]=> string(27) "omiya" [ "term_group" ]=> int(0) [ "term_taxonomy_id" ]=> int(14) [ "taxonomy" ]=> string(9) "area" [ "description" ]=> string(0) "" [ "parent" ]=> int(0) [ "count" ]=> int(1) [ "object_id" ]=> int(70) [ "filter" ]=> string(3) "raw" } [2]=> object(stdClass)#xxxx (11) { [ "term_id" ]=> int(13) [ "name" ]=> string(18) "池袋" [ "slug" ]=> string(54) "ikebukuro" [ "term_group" ]=> int(0) [ "term_taxonomy_id" ]=> int(13) [ "taxonomy" ]=> string(9) "area" [ "description" ]=> string(0) "" [ "parent" ]=> int(0) [ "count" ]=> int(2) [ "object_id" ]=> int(70) [ "filter" ]=> string(3) "raw" } } |
get_term_by
タームのid、スラッグ、名前などからタームの全データをデータベースから取得します。
1 | <?php get_term_by( $field , $value , $taxonomy , $output , $filter ) ?> |
パラメーター
- $field
- (文字列)(必須) ‘id’, ‘slug’, ‘name’, または ‘term_taxonomy_id’ の何れか。初期値: ‘id’
- $value
- (文字列|整数)(必須) この値を検索。
- $taxonomy
- (文字列) (必須) タクソノミー名。category, post_tag, link_category またはカスタム分類の名前。
戻り値
- term_id
- name
- slug
- term_group
- term_taxonomy_id
- taxonomy
- description
- parent
- count
「$term」はデフォルトでタクソノミーのスラッグ名を出力するので、下記のようにスラッグからタームの情報を取得することができます。アーカイブページでも使用可能です。
1 | $term_info = get_term_by( "slug" , $term , "タクソノミー名" ); |
get_term_children
指定されたタームの子ターム情報の全てをひとつの配列にまとめます。 階層のあるタクソノミーについてのみ有用です。もしタームが指定されたタクソノミーに無ければ、空の配列を返します。
1 | <?php get_term_children( $term , $taxonomy ) ?> |
パラメーター
- $term
- (文字列|整数)(必須)子タームを取得するタームのID
- $taxonomy
- (文字列)(必須)タクソノミーの名前
the_terms
投稿記事に付けられたタームを文字列として表示します。ユーザー定義の文字で区切ったカスタム分類のタームにリンクを付けて表示し、戻り値はありません。
1 | <?php the_terms( $id , $taxonomy , $before , $sep , $after ); ?> |
パラメーター
- id
- (整数)(必須) 投稿 ID
- $taxonomy
- (文字列)(必須) タクソノミーの名前。
- $before
- (文字列)(オプション) 先頭のタームの前に表示する文字列。
- $sep
- (文字列)(オプション) ターム(およびリンク)を区切る文字列。初期値: ‘, ‘
- $after
- (文字列)(オプション) 最後のタームに続けて表示する文字列。
サンプルコード
現在の投稿のカテゴリーをリスト表示する→「カテゴリー: 代官山 / 大宮 / 池袋」
1 | <?php the_terms( $post ->ID, 'area' , 'カテゴリー: ' , ' / ' ); ?> |
single_term_title
現在のページのタームタイトルを表示または取得します。通常はタクソノミー(ターム)アーカイブページで使用します。
1 | <?php single_term_title( $prefix , $display ); ?> |
パラメーター
- $prefix
- (文字列)(オプション) タイトルの前に出力するテキスト。
- display
- (真偽値)(オプション) タイトルを表示する(TRUE)、PHP で使えるようにタイトルを返す(FALSE)
get_the_term_list
指定されたタクソノミー(カスタム分類)に関して、投稿に付けられたタームの HTML 文字列を返します。 タームの文字列はそのタームのアーカイブページへリンクします。
1 | <?php get_the_term_list( $id , $taxonomy , $before , $sep , $after ) ?> |
パラメーター
- $id
- (整数)(必須)投稿ID
- $taxonomy
- (文字列)(必須)タクソノミーの名前
- $before
- (文字列)(オプション)前に入れる文字列
- $sep
- (文字列)(オプション)タームを区切る文字列
- $after
- (文字列)(オプション)後に続く文字列
get_term_link
指定された(タクソノミー)タームのアーカイブページへのパーマリンクを返します。 タームが存在しないときは WP_Error オブジェクトを返します。
1 | <?php get_term_link( $term , $taxonomy ); ?> |
パラメーター
- $term
- (オブジェクト|整数|文字列)(必須)リンクを取得するタームのオブジェクト、ID またはスラッグ。
- $taxonomy
- (文字列)(オプション)タクソノミーのスラッグ。$term がオブジェクトのときはオプション。
the_taxonomies
このテンプレートタグをループの中に記述すると、投稿に関連付けたカテゴリーおよびカスタム分類をリンク有りの状態で表示します。 また、ループ外に記述した場合は、ループの最終投稿に関連づいたカテゴリーおよびカスタム分類を表示します。
1 | <?php the_taxonomies( $args ); ?> |
wp_get_object_terms
指定されたオブジェクト(複数でもよい)に付いている、指定されたタクソノミー(カスタム分類)のタームを取得します。
1 | <?php wp_get_object_terms( $object_ids , $taxonomies , $args ); ?> |
パラメーター
- $object_ids
- (文字列|配列)(必須) タームを取得するオブジェクトのID 。
- $taxonomies
- (文字列|配列)(必須) タームを取得するタクソノミー。
- $args
- (配列|文字列)(オプション) 戻り値の種類を指定。