要测试帖子是否为任何自定义帖子类型,请获取所有非内置帖子类型的列表,并测试帖子的类型是否在该列表中。
作为一种功能:
/**
* 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!\';