Warning: Trying to access array offset on false in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/wp.php on line 216
get_post_type() - 检索当前或给定文章的文章类型 - WordPress函数 - WordPress动力
当前浏览:首页 / WordPress函数 / get_post_type()

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

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


Warning: Undefined array key -1 in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/reference-cat.php on line 185

Warning: Undefined array key 3 in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/reference-cat.php on line 186

Warning: Undefined variable $searchand in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/wp.php on line 154

Warning: Undefined array key "b2f7dfdca5567db6f986bb6af13827bd" in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/wp.php on line 223

post_type 文章类型more...


参数

$post

(int|WP_Post|null) (可选) 文章 ID或Post对象,默认为全局$post。

默认值: null


返回

(string|false) 成功时返回文章类型,失败时为false。



源码

查看源码 官方文档


更新日志

版本描述
2.1.0开始引入

使用示例

  • 示例1

    默认WordPress文章类型名称
    以下是默认的文章类型名称,将其用作此函数的参数,以收集文章类型对象

    post
    page
    attachment
    revision
    nav_menu_item

  • 示例2

    条件语句,判断它是否是某个文章类型。

    if ( get_post_type( get_the_ID() ) == 'slug_post_type' ) {
        //if is true
    }
    
  • 示例3

    显示文章类型,这个例子需要在循环中

    printf( __( 'The post type is: %s', 'textdomain' ), get_post_type( get_the_ID() ) );
    
  • 示例4

    在某些情况下,例如当您在循环外部时,您可能需要传递get_queried_object_id()作为参数,以获取当前查询对象的文章类型:

    $current_queried_post_type = get_post_type( get_queried_object_id() );