dd

wordpress获取文章类型函数:get_post_type()


【说明】
检索当前文章或给定文章的文章类型。
【用法】

<?php get_post_type( $post ) ?>

【参数】
$post

(混合)(可选)文章对象或文章ID。
默认值: false
【返回的值】
(布尔型| 字符)

返回文章类型,出错时返回false。
【示例】

<?php 
$post_type =get_post_type(36);
//假设id为36的文章类型为附件类型attachment
echo $post_type;
//打印出attachment
?>

【注释】

用法:$posts 日志循环的全局变量
【修改记录】
自2.1.0版本后
【源文件】
get_post_type() 位于wp-includes/post.php中

/**
 * Retrieve the post type of the current post or of a given post.
 *
 * @since 2.1.0
 *
 * @uses $post The Loop current post global
 *
 * @param mixed $the_post Optional. Post object or post ID.
 * @return bool|string post type or false on failure.
 */
function get_post_type( $the_post = false ) {
         global $post;
 
         if ( false === $the_post )
                   $the_post = $post;
         elseif ( is_numeric($the_post) )
                   $the_post = get_post($the_post);
 
         if ( is_object($the_post) )
                   return $the_post->post_type;//返回文章的类型post_type
 
         return false;
}