24

How can I get the current custom post type name and echo it on a page?

3 Answers 3

46

You'll need the post object somehow, or, alternatively the queried object on post type archives. On a singular page you might do:

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

Or in the loop:

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

In post type archives:

$postType = get_queried_object();
echo esc_html($postType->labels->singular_name);

All of these will give you the singular name of the post type that was registered in register_post_type's labels key.

2
  • Can you help me here. I use the following code now: snippi.com/s/wookr64 With this code I want to call only the current posttypes on the page. Now it doesn’t work correctly. What does I wrong?
    – Casper
    Commented Nov 25, 2014 at 20:34
  • 2
    @Casper I think what you will need to do, go and sit down, and reconstruct your previous question. It is still totally unclear what you want. Why did this answer on SO not work. Explain with examples and template names exactly what you want. Thank you Commented Nov 26, 2014 at 5:00
7

To get the name of the current post type, use the following code inside the loop.

$post_type = get_post_type( get_the_ID() );
echo '<p>' . $post_type . '</p>';

or display the post type name using printf function.

printf( __( 'The post type is: %s', 'textdomain' ), get_post_type( get_the_ID() ) );
1

The method get_post_type() retrieves the post type of the current post or of a given post. More info here.

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.