Created
May 2, 2019 22:58
-
-
Save VermillionOne/0f05f71f63b60e5fc4a44ee21b505a56 to your computer and use it in GitHub Desktop.
Check If Custom Post Type
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
/** | |
* Check if a post is a custom post type. | |
* @param mixed $post Post object or ID | |
* @return boolean | |
*/ | |
function is_custom_post_type( $post = NULL ) | |
{ | |
$all_custom_post_types = get_post_types( array ( '_builtin' => FALSE ) ); | |
// there are no custom post types | |
if ( empty ( $all_custom_post_types ) ) | |
return FALSE; | |
$custom_types = array_keys( $all_custom_post_types ); | |
$current_post_type = get_post_type( $post ); | |
// could not detect current type | |
if ( ! $current_post_type ) | |
return FALSE; | |
return in_array( $current_post_type, $custom_types ); | |
} | |
if ( is_custom_post_type() ) | |
print 'This is a custom post type!'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Credit:
https://wordpress.stackexchange.com/users/73/fuxia
https://wordpress.stackexchange.com/questions/6731/if-is-custom-post-type/95906#95906