6

Let's say I have a Custom Post Type with the slug books. The label is Readings and the singular label is Reading.

I want to display the Custom Post Type label in one of the custom post type's post. How can I achieve that?

If I want to display a page title from its slug, I can use

echo get_the_title(get_page_by_path('other-page-slug'));

But I haven't found a clue to do this with custom post type.

1
11

get_post_type_object() will return, as the name suggests, an object that contains the post type information.

You may want to var_dump() it to inspect it contents. You'll see that it includes (among other stuff) another object, labels that contains all the registered labels for the specific post type.

$pt = get_post_type_object( 'books' );

// These two usually contain the post type name in plural. 
// They may differ though.
echo $pt->label;
echo $pt->labels->name;

// This one holds the post type name in singular.
echo $pt->labels->singular_name;
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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