Last Updated: September 09, 2019
·
2.239K
· bainternet

Simple Function to check for post type

Here is a simple usful function i use to check for a specific post type:

function is_post_type($type='post') {
    global $post_type,$typenow;

    if (isset( $_GET['post_type'] )&& $_GET['post_type'] == $type 
        ||  isset( $post_type ) && $post_type == $type) 
        ||  isset( $typenow ) && $typenow == $type) 
        return true; // return true if on a page of type $type

    return false;
}

usage:

if (is_post_type('slider')){
    // the post type is slider :)
}

1 Response
Add your response

WordPress provides the is_singular() function for checking the current post type(s):

http://codex.wordpress.org/Function_Reference/is_singular

Hope it's useful :)

over 1 year ago ·

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

PHP applications still send a browser full of JavaScript. Hardening the server does not inventory the payment, analytics, and plugin scripts that run after the HTML is delivered.

Security insights from Cside