WordPress函数:post_type_exists 文章类型是否已注册

追格官方小助手/ 2022年02月20日/ WordPress/ 浏览 1282

在WordPress主题开发中可以使用 post_type_exists 检查文章类型是否已注册。

函数源码:

function post_type_exists( $post_type ) {
    return (bool) get_post_type_object( $post_type );
}

可以看出,就是用 get_post_type_object 查找了一下。

使用举例:

$exists = post_type_exists( 'post' );
// returns true
  
$exists = post_type_exists( 'page' );
// returns true
  
$exists = post_type_exists( 'book' );
// returns true if book is a registered post type
  
$exists = post_type_exists( 'xyz' );
// returns false if xyz is not a registered post type

发表评论

暂无评论,抢个沙发...

客服 工单