最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Get custome post type name

programmeradmin3浏览0评论

I have a custom post type named 'industry', I tried to use get_post_type_object('industry')->labels->name to get the name but end up return empty value, what is wrong?

This is the registered post type:

// Register industry post type
function _ws_industry_post_type() {
  $labels = array(
    'name' => 'Industries',
    'singular_name' => 'Industry',
    'add_new_item' => 'Add New Industry',
    'edit_item' => 'Edit Industry',
    'new_item' => 'New Industry',
    'view_item' => 'View Industry',
    'search_items' => 'Search Industries',
    'not_found' => 'No industries found',
    'not_found_in_trash' => 'No industries found in Trash',
    'parent_item_colon' => 'Parent Industry:',
    'all_items' => 'All Industries',
    'archives' => 'Industry Archives',
    'insert_into_item' => 'Insert into industry',
    'uploaded_to_this_item' => 'Uploaded to this industry',
    'featured_image' => 'Featured image',
    'set_featured_image' => 'Set featured image',
    'remove_featured_image' => 'Remove featured image',
    'use_featured_image' => 'Use as featured image'
);
  $args = array(
    'labels' => $labels,
    'description' => 'Sortable/filterable industries',
    'public' => true,
    'exclude_from_search' => false,
    'publicly_queryable' => true,
    'show_ui' => true,
    'show_in_nav_menus' => true,
    'show_in_menu' => true,
    'show_in_admin_bar' => true,
    'menu_position' => 20 ,
    'menu_icon' => 'dashicons-chart-line',
    'capability_type' => 'post',
    'hierarchical' => false,
    'supports' => array('title', 'thumbnail'),
    'register_meta_box_cb' => null,
    'taxonomies' => array(),
    'has_archive' => false,
    'rewrite' => array('slug' =>    'industries'),
    'query_var' => true,
    'can_export' => true
);
  register_post_type('industry', $args);
}
add_action('init', '_ws_industry_post_type');

I have a custom post type named 'industry', I tried to use get_post_type_object('industry')->labels->name to get the name but end up return empty value, what is wrong?

This is the registered post type:

// Register industry post type
function _ws_industry_post_type() {
  $labels = array(
    'name' => 'Industries',
    'singular_name' => 'Industry',
    'add_new_item' => 'Add New Industry',
    'edit_item' => 'Edit Industry',
    'new_item' => 'New Industry',
    'view_item' => 'View Industry',
    'search_items' => 'Search Industries',
    'not_found' => 'No industries found',
    'not_found_in_trash' => 'No industries found in Trash',
    'parent_item_colon' => 'Parent Industry:',
    'all_items' => 'All Industries',
    'archives' => 'Industry Archives',
    'insert_into_item' => 'Insert into industry',
    'uploaded_to_this_item' => 'Uploaded to this industry',
    'featured_image' => 'Featured image',
    'set_featured_image' => 'Set featured image',
    'remove_featured_image' => 'Remove featured image',
    'use_featured_image' => 'Use as featured image'
);
  $args = array(
    'labels' => $labels,
    'description' => 'Sortable/filterable industries',
    'public' => true,
    'exclude_from_search' => false,
    'publicly_queryable' => true,
    'show_ui' => true,
    'show_in_nav_menus' => true,
    'show_in_menu' => true,
    'show_in_admin_bar' => true,
    'menu_position' => 20 ,
    'menu_icon' => 'dashicons-chart-line',
    'capability_type' => 'post',
    'hierarchical' => false,
    'supports' => array('title', 'thumbnail'),
    'register_meta_box_cb' => null,
    'taxonomies' => array(),
    'has_archive' => false,
    'rewrite' => array('slug' =>    'industries'),
    'query_var' => true,
    'can_export' => true
);
  register_post_type('industry', $args);
}
add_action('init', '_ws_industry_post_type');
Share Improve this question asked May 18, 2020 at 4:15 kefosekikefoseki 1
Add a comment  | 

1 Answer 1

Reset to default 0

Try this code

$post = get_queried_object();
$postType = get_post_type_object(get_post_type($post));
if ($postType) {
    echo esc_html($postType->labels->singular_name);
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论