获取自定义帖子类型名称返回空

问题描述 投票:0回答:1

我有一个名为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');
wordpress custom-post-type
1个回答
0
投票

我认为您没有使用menu_name。

您可以尝试:

function Industry() {
$labels = array(
'name'               => __( 'industry'),
'singular_name'      => __( 'industry' ),
'add_new'            => __( 'add industry'),
'add_new_item'       => __( 'add industry' ),
'edit_item'          => __( 'edit' ),
'new_item'           => __( 'new industry ' ),
'all_items'          => __( ' all ' ),
'view_item'          => __( 'view ' ),
'search_items'       => __( 'search ' ),
'not_found'          => __( 'No industries found' ),
'not_found_in_trash' => __( 'No industries found' ),
'parent_item_colon'  => '',
'menu_name'          => 'industry'
);
$args = array(
'labels'        => $labels,
'description'   => 'Save Industry',
'public'        => true,
'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
'has_archive'   => true,
);
register_post_type( 'Industry', $args );
}
add_action( 'init', 'Industry' );
© www.soinside.com 2019 - 2024. All rights reserved.