Get custome post type name

时间:2020-05-18 作者:kefoseki

我有一个名为“industry”的自定义帖子类型,我尝试使用get_post_type_object(\'industry\')->labels->name 要获取名称但最终返回空值,有什么问题吗?

这是注册职位类型:

// 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\');

1 个回复
SO网友:Tanmay Patel

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);
}

相关推荐