Skip to content

Instantly share code, notes, and snippets.

@joshuadavidnelson
Last active January 2, 2016 10:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save joshuadavidnelson/8291503 to your computer and use it in GitHub Desktop.
Save joshuadavidnelson/8291503 to your computer and use it in GitHub Desktop.
Checks if the current post is a custom post type, returns boolean
<?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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment