Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a all my custom post types list together in blog format. What I'm trying to do is echo the post type name on each post.

I tried this:

get_post_type_object('post');
echo $obj->labels->singular_name;

But it just displayed "Post" for everything instead of the custom post type name

share|improve this question
add comment

3 Answers 3

up vote 5 down vote accepted

If you are within The Loop, try:

$post_type = get_post_type( $post->ID );
echo $post_type;

Does this work for you?

share|improve this answer
    
That worked perfectly! Thanks! –  kel Jan 31 '11 at 1:38
    
Want to give me a +1 and the check mark? ;-) –  epaps Jan 31 '11 at 3:18
add comment

Inside the loop you already have a $post object available to you, you don't need to call get_post_type or any other functions, this should do the trick.

<?php echo $post->post_type; ?>

Or if you like the idea of calling get_post_type you can make the task easier for the function by passing it the post object you have(so it doesn't need to go fetch the post, just to extract one property from it).

<?php echo get_post_type( $post ); ?>

Else, get_post_type will needlessly call get_post to fetch the post object (an object you already have available to you nonetheless).

Hope that helps..

share|improve this answer
    
That works too, thanks! –  kel Feb 1 '11 at 20:37
    
Once a post is fetched from the DB calling get_post() again isn't a serious issue. It's all cached internally for the duration of the pageview, the only overhead is a fast function call to retrieve the object from cache. In this case get_post_type() actually does pretty much nothing beyond return $post->post_type so your optimization actually makes sense, but it should probably have a filter or something, in which case using get_post_type() would be the API way to go. –  jeremyclarke Sep 21 '12 at 19:20
add comment

Reading the docs at http://codex.wordpress.org/Function_Reference/get_post_type_object you must pass it the custom post type as argument!

Try with:

get_post_type_object('your-post-type');
echo $obj->labels->singular_name;
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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