WordPress函数get_post_status()获取文章状态

描述:

获取文章的状态

用法:

<?php get_post_status( $ID ) ?>

参数:

$ID

(integer) (可选) 文章ID

默认值: ”

示例:

<?php
	if ( get_post_status ( $ID ) == 'private' ) {
		echo 'private';
	} else {
		echo 'public';
	}
?>

源文件:

/**
 * Retrieve the post status based on the Post ID.
 *
 * If the post ID is of an attachment, then the parent post status will be given
 * instead.
 *
 * @since 2.0.0
 *
 * @param int|WP_Post $ID Optional. Post ID or post object. Default empty.
 * @return string|false Post status on success, false on failure.
 */
function get_post_status( $ID = '' ) {
	$post = get_post($ID);

	if ( !is_object($post) )
		return false;

	if ( 'attachment' == $post->post_type ) {
		if ( 'private' == $post->post_status )
			return 'private';

		// Unattached attachments are assumed to be published.
		if ( ( 'inherit' == $post->post_status ) && ( 0 == $post->post_parent) )
			return 'publish';

		// Inherit status from the parent.
		if ( $post->post_parent && ( $post->ID != $post->post_parent ) ) {
			$parent_post_status = get_post_status( $post->post_parent );
			if ( 'trash' == $parent_post_status ) {
				return get_post_meta( $post->post_parent, '_wp_trash_meta_status', true );
			} else {
				return $parent_post_status;
			}
		}

	}

	return $post->post_status;
}