post_type_exists()判断某个文章类型是否存在

目录

描述

This Conditional Tag checks if a post type exists by passing a post type name an argument. This is a boolean function and uses a global $wp_post_types variable for checking the post type existence, meaning it returns either TRUE if the post type exists or FALSE if it doesn't exist. The post type must be a registered post type.

post_type_exists() 描述

用法

<?php post_type_exists$post_type ); ?>

post_type_exists() 用法

参数

$post_type

(string) (必填) Post type.

默认值: None

post_type_exists() 参数

示例

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

if ( post_type_exists( 'book' ) ) {
   echo 'the Book post type exists';
}
/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

$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

post_type_exists() 示例

历史

  • 添加于 版本 3.0

post_type_exists() 历史

源文件

post_type_exists() 函数的代码位于 wp-includes/post.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Check if a post type is registered.
 *
 * @since 3.0.0
 *
 * @see get_post_type_object()
 *
 * @param string $post_type Post type name.
 * @return bool Whether post type is registered.
 */
function post_type_exists( $post_type ) {
	return (bool) get_post_type_object( $post_type );
}

post_type_exists() 源文件

相关