カスタム投稿タイプを使っている場合において、現在のカスタム投稿タイプの名前などを取得するには以下の方法で行ないます。
1 2 | $customPostTypeObj = get_post_type_object(get_post_type()); return esc_html($customPostTypeObj->labels->name); |
この方法はたまたま発見した以下のページが役に立ちました。
WordPress › Support » error when trying to use get_post_type_labels on custom post type
get_post_type_object() の引数にカスタム投稿タイプ名を入れるとカスタム投稿タイプのオブジェクトを生成することができるわけですが、どんなオブジェクトが生成されるのか、中を覗いてみると以下のようになっています。
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 | stdClass Object ( [labels] => stdClass Object ( [name] => ニュース [singular_name] => ニュース [add_new] => ニュースを追加 [add_new_item] => 新しいニュースを追加 [edit_item] => ニュースを編集 [new_item] => 新しいニュース [view_item] => ニュースを編集 [search_items] => ニュースを探す [not_found] => ニュースはありません [not_found_in_trash] => ゴミ箱にニュースはありません [parent_item_colon] => ) [description] => [publicly_queryable] => 1 [exclude_from_search] => [_builtin] => [_edit_link] => post.php?post=%d [capability_type] => post [hierarchical] => [public] => 1 [rewrite] => Array ( [slug] => news [with_front] => 1 ) [query_var] => news [register_meta_box_cb] => [taxonomies] => Array ( [0] => news_type ) [show_ui] => 1 [menu_position] => 2 [menu_icon] => [permalink_epmask] => 1 [can_export] => 1 [show_in_nav_menus] => 1 [name] => news [cap] => stdClass Object ( [edit_post] => edit_post [edit_posts] => edit_posts [edit_others_posts] => edit_others_posts [publish_posts] => publish_posts [read_post] => read_post [read_private_posts] => read_private_posts [delete_post] => delete_post ) [label] => ニュース ) |
$customPostTypeObj->taxonomies なんてかなり役に立つのでは無いでしょうか。
コメント