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

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

相关函数:

阿里云