函数文档

post_type_exists()

💡 云策文档标注

概述

post_type_exists() 函数用于检查指定的文章类型是否已在 WordPress 中注册。它基于 get_post_type_object() 实现,返回布尔值。

关键要点

  • 函数接受一个字符串参数 $post_type,表示文章类型名称。
  • 返回布尔值:true 表示文章类型已注册,false 表示未注册。
  • 内部实现通过调用 get_post_type_object() 并转换为布尔值来判断。
  • 相关函数包括 get_post_type_object()、unregister_post_type() 等,用于文章类型操作。
  • 自 WordPress 3.0.0 版本引入。

代码示例

$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

📄 原文内容

Determines whether a post type is registered.

Description

For more information on this and similar theme functions, check out the Conditional Tags article in the Theme Developer Handbook.

See also

Parameters

$post_typestringrequired
Post type name.

Return

bool Whether post type is registered.

Source

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

Changelog

Version Description
3.0.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Basic Examples

    $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