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

1 Answer 1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.