Wednesday, July 31, 2013

WordPress function to check if the current post is a custom post type

WordPress function to check if the current post is a custom post type


Just paste this code into your functions.php file:


function is_custom_post_type() {
    global $wp_query;
       
    $post_types = get_post_types(array('public'   => true,'_builtin' => false),'names','and');
   
    foreach ($post_types  as $post_type ) {
        if (get_post_type($post_type->ID) == get_post_type($wp_query->post->ID)) {
            return true;
        } else {
            return false;
        }
    }
}

Once complete, we can use the function as display below.

P.S. : function can be used outside the loop:

if (is_custom_post_type()) {
    //Current post is a custom post type
}

I just got my #domain @BigRock. Get upto 25% off with my personalized coupon link Get upto 25% off