カスタム投稿に設定したカスタム分類のカテゴリー名を取得するやり方です。
ループ外で『$test = get_taxonomies(array(‘public’ => true, ‘_builtin’ => false), ‘objects’);』を実行する。
‘public’ => true は 公開しているものだけ
‘_builtin’ => false は 既存のもの以外
という設定なので、
以下のようにカスタム分類のオブジェクトが$testに返ります。
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
Array ( [bookCat] => WP_Taxonomy Object ( [name] => bookCat [label] => 本のカテゴリー [labels] => stdClass Object ( [name] => 本のカテゴリー [singular_name] => 本のカテゴリー [search_items] => カテゴリーを検索 [popular_items] => [all_items] => 本のカテゴリー [parent_item] => 親カテゴリー [parent_item_colon] => 親カテゴリー: [edit_item] => カテゴリーの編集 [view_item] => カテゴリーを表示 [update_item] => カテゴリーを更新 [add_new_item] => 新規カテゴリーを追加 [new_item_name] => 新規カテゴリー名 [separate_items_with_commas] => [add_or_remove_items] => [choose_from_most_used] => [not_found] => カテゴリーが見つかりませんでした。 [no_terms] => カテゴリーなし [items_list_navigation] => カテゴリーリストナビゲーション [items_list] => カテゴリーリスト [menu_name] => 本のカテゴリー [name_admin_bar] => 本のカテゴリー [archives] => 本のカテゴリー ) [description] => [public] => 1 [publicly_queryable] => 1 [hierarchical] => 1 [show_ui] => 1 [show_in_menu] => 1 [show_in_nav_menus] => 1 [show_tagcloud] => 1 [show_in_quick_edit] => 1 [show_admin_column] => [meta_box_cb] => post_categories_meta_box [object_type] => Array ( [0] => book ) [cap] => stdClass Object ( [manage_terms] => manage_categories [edit_terms] => manage_categories [delete_terms] => manage_categories [assign_terms] => edit_posts ) [rewrite] => Array ( [with_front] => 1 [hierarchical] => [ep_mask] => 0 [0] => [slug] => bookcat ) [query_var] => bookcat [update_count_callback] => [show_in_rest] => [rest_base] => [rest_controller_class] => [_builtin] => [singular_label] => 本のカテゴリー ) ) |
この例では
『register_post_type(‘book’, array(・・・』
『register_taxonomy(‘bookCat’, ‘book’, array(』
と設定したカスタム投稿が取得されています。
ここからは簡単で、
『$test -> name』とすれば『bookCat』が取れました。