7

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

21

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.

  • 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 Nov 25 '14 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 – Pieter Goosen Nov 26 '14 at 5:00
3

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

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.