Get post type in header.php

you can use get_queried_object, as get_post_type does not work on custom post type (I don’t know yet why, more info here)

 $obj = get_queried_object();
 $custom_post_type = $obj->post_type;

then you can proceed with

 if($custom_post_type == "type1"){
    // do something
 } else {
    // etc..
 }

I’ve tested this in one of the sites I’m working on, I hope it work on yours too..

EDIT:

here is the new code that you can use for archives too:

global $wp_query;

if (is_archive()):  
    $custom_post_type = get_query_var('post_type');
    $custom_taxonomy = get_query_var('taxonomy');
else:
    $obj = get_queried_object();
    $custom_post_type = $obj->post_type;
endif;

I’ve added a taxonomy type too in case you would need one yourself.

Leave a Comment