Checks if the current post is a custom post type, returns boolean. Very handy when working with custom post types, layouts and the like.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Checks if current post is a custom post type, returns boolean | |
* | |
* @author Joshua David Nelson | |
* @link http://joshuadnelson.com/code/is_custom_post_type/ | |
*/ | |
if ( !function_exists( 'is_custom_post_type' ) ) { | |
function is_custom_post_type() { | |
// find custom post types | |
$args = array( | |
//'public' => true, // -- uncomment if you only want "public" cpts | |
'_builtin' => false, | |
); | |
$output = 'names'; | |
$public_custom_post_types = get_post_types( $args, $output ); | |
// if there are no custom post types, then the current post can't be one | |
if( empty( $public_custom_post_types ) ) | |
return false; | |
// access global variable | |
global $post; | |
// get the current post type, returns a string or false on failure | |
$post_type = get_post_type( $post ); | |
// check if current post type is a custom post type | |
if ( $post_type && in_array( $post_type, $public_custom_post_types ) ) { | |
return true; | |
} else { | |
return false; | |
} | |
// if all else fails, return false | |
return false; | |
} | |
} |
Source: https://gist.github.com/joshuadavidnelson/8291503
Tags: conditional, custom post type, page type