函数文档

get_post_type()

💡 云策文档标注

概述

get_post_type() 函数用于检索当前文章或指定文章的文章类型。它接受一个可选参数,返回文章类型字符串或失败时的 false。

关键要点

  • 参数 $post 可选,可以是文章 ID、WP_Post 对象或 null,默认为全局 $post
  • 返回值:成功时返回文章类型字符串,失败时返回 false
  • 内部实现基于 get_post() 获取文章对象,然后访问其 post_type 属性
  • 常用于条件判断、显示文章类型或与其他函数结合使用

代码示例

// 示例1:检查当前文章是否为特定类型
if ( get_post_type() === 'slug_post_type' ) {
    // 条件成立时的操作
}

// 示例2:显示当前文章类型(需在循环内)
printf( __( 'The post type is: %s', 'textdomain' ), get_post_type( get_the_ID() ) );

// 示例3:在循环外获取当前查询对象的文章类型
$current_queried_post_type = get_post_type( get_queried_object_id() );

// 示例4:检查文章是否属于特定类型数组
function check_post_in_portfolio_or_product( $post_id ) {
    $post_type = get_post_type( $post_id );
    if ( in_array( $post_type, array( 'portfolio', 'product' ), true ) ) {
        return true;
    }
    return false;
}

注意事项

  • 默认 WordPress 文章类型包括:post、page、attachment、revision、nav_menu_item
  • 注意区分文章类型(post type)与文章格式(post format),两者概念不同
  • 在循环外使用时,可能需要传递 get_queried_object_id() 作为参数以获取正确文章类型

📄 原文内容

Retrieves the post type of the current post or of a given post.

Parameters

$postint|WP_Post|nulloptional
Post ID or post object. Default is global $post.

Default:null

Return

string|false Post type on success, false on failure.

Source

function get_post_type( $post = null ) {
	$post = get_post( $post );
	if ( $post ) {
		return $post->post_type;
	}

	return false;
}

Changelog

Version Description
2.1.0 Introduced.

User Contributed Notes

  1. Skip to note 8 content

    Conditional sentence to see if it is a post type.

    if ( get_post_type( get_the_ID() ) == 'slug_post_type' ) {
        //if is true
    }

  2. Skip to note 11 content

    Returns true if the given post ID is associated with a portfolio or product

    <br />
    function check_post_in_portfolio_or_product( $post_id ) {<br />
    $post_type = get_post_type( $post_id ); </p>
    <p> if ( in_array( $post_type, array( 'portfolio', 'product' ), true ) ) {<br />
    return true;<br />
    }</p>
    <p> return false;<br />
    }</p>
    <p>Example:</p>
    <p>$post_id = 123;<br />
    if ( check_post_in_portfolio_or_product( $post_id ) ) {<br />
    // Do something only if it's a portfolio or product post.<br />
    }<br />

  3. Skip to note 12 content

    get_post_type();
    is commonly used in conjunction with Post Formats.
    Functionality is extended in themes by including:
    add_theme_support( 'post-formats', array( 'aside', 'gallery' ) );
    in your functions.php file.

    After including this in your theme’s functions.php file, the option to choose a post type (that you included inside the array) will appear in the right sidebar when creating/editing a post.

    If no Post Type is selected, WordPress selects the default which is Standard.

    WordPress supported post formats (that are not defaults) are:
    'aside', 'gallery', 'link', 'image', 'quote', 'status', 'video', 'audio', 'chat'

    Everything you need to know about using & styling Post Formats is here: https://codex.wordpress.org/Post_Formats