Tell me more ×
WordPress Answers 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

1 Answer

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

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.