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.

How do I discover the custom post type slug when I'm on an archive page?

For instance if /products/ fires the archive-products.php template, how (pragmatically) do I get the post type slug?

Thanks

share|improve this question

2 Answers 2

To get the current post type use get_post_type(). Then ask get_post_type_object() for all the data you need, for example the slug:

$post_type = get_post_type();
if ( $post_type )
{
    $post_type_data = get_post_type_object( $post_type );
    $post_type_slug = $post_type_data->rewrite['slug'];
    echo $post_type_slug;
}
share|improve this answer
    
I think (hadn't tested) get_queried_object() would get to same info in less moves. –  Rarst Oct 9 '12 at 1:15
    
@Rarst Maybe, but I think the code I suggested is easier to understand. –  toscho Oct 9 '12 at 1:18
    
Toscho's solutions is wrong, because get_post_type returns the post type of current page, and, when you are into archive page, this function alwasy return "page". I'm sarching to solve the same: When I am into archive page of 'books' (4 example), I want this: 'books'. When I get it I'll post it. –  eMarine Jun 25 '13 at 9:58
if( get_post_type( get_the_ID() ) == 'projects' )
{
  //enter code for this post type
}
share|improve this answer

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.