GeekerCode更好的WordPress主题,值得信任的WordPress主题开发商

极美的主题、极致的插件

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

摘要:【说明】检索当前文章或给定文章的文章类型。【用法】<?php get_post_type( $post ) ?>【参数】$post(混合)(可选)文章对象或文章ID。默认值: false【返回的值】(布尔型| 字

【说明】

检索当前文章或给定文章的文章类型。

【用法】

<?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;
}

建站!你有充足的理由选择我们

世界上超过30%的网站是由WordPress搭建,而我们是国内最靠谱的WordPress主题开发商
QQ咨询在线咨询问答互助微信号geekercode微信公众号云服务器