WordPress根据文章ID获取文章类型函数:get_post_type

文 / @WordPress主题

WordPress函数get_post_type获取当前文章或指定文章的文章类型,在The Loop主循环中使用则不需要传递文章ID。

get_post_type( int|WP_Post|null $post = null )

函数参数

$post

整数或WP_Post对象

传递文章ID或WP_Post对象

函数使用示例

$post_id = get_the_ID();
if ( get_post_type( $post_id ) == 'page' ) {
	echo "文章{$post_id}的类型为page";
}

扩展阅读

get_post_type()函数位于:wp-includes/post.php

相关函数:

  • get_post()
  • get_post_types()
添加UTHEME为好友
扫码添加UTHEME微信为好友
· 分享WordPress相关技术文章,主题上新与优惠动态早知道。
· 微信端最大WordPress社群,限时免费入群。