函数文档

post_type_supports()

💡 云策文档标注

概述

post_type_supports() 函数用于检查指定文章类型是否支持特定功能。它接受两个参数:文章类型和功能字符串,返回布尔值表示支持状态。

关键要点

  • 函数参数:$post_type(字符串,必需)指定要检查的文章类型;$feature(字符串,必需)指定要检查的功能。
  • 返回值:布尔值,表示文章类型是否支持该功能,不支持时返回 false。
  • 支持的功能字符串包括:'title'、'editor'、'author'、'thumbnail'、'excerpt'、'trackbacks'、'custom-fields'、'comments'、'revisions'、'page-attributes'、'post-formats',其中 'thumbnail' 还支持 'attachment:audio' 和 'attachment:video'。
  • 注意事项:检查未定义的功能字符串将返回 false;'thumbnail' 功能需要当前主题支持 Post Thumbnails;'page-attributes' 要求文章类型为 hierarchical。

代码示例

// 检查文章类型 'post' 是否支持 'comments'
if ( post_type_supports( 'post', 'comments' ) ) {
    // 执行相关代码
}

// 检查文章类型 'page' 是否支持 'excerpt'
if ( post_type_supports( 'page', 'excerpt' ) ) {
    // 执行相关代码
}

📄 原文内容

Checks a post type’s support for a given feature.

Parameters

$post_typestringrequired
The post type being checked.
$featurestringrequired
The feature being checked.

Return

bool Whether the post type supports the given feature.

More Information

The $feature variable in the function will accept the following string values:

  • ‘title’
  • ‘editor’ (content)
  • ‘author’
  • ‘thumbnail’ (featured image) (current theme must also support Post Thumbnails)
  • ‘excerpt’
  • ‘trackbacks’
  • ‘custom-fields’ (see Custom_Fields, aka meta-data)
  • ‘comments’ (also will see comment count balloon on edit screen)
  • ‘revisions’ (will store revisions)
  • ‘page-attributes’ (template and menu order) (hierarchical must be true)
  • ‘post-formats’ (see Post_Formats)

Please note in the ‘thumbnail’ value you can also use ‘attachment:audio’ and ‘attachment:video’ .

If the value is not available it will return false.

You can send any string in this field and it will return false unless you are checking for an accepted value.

Source

function post_type_supports( $post_type, $feature ) {
	global $_wp_post_type_features;

	return ( isset( $_wp_post_type_features[ $post_type ][ $feature ] ) );
}

Changelog

Version Description
3.0.0 Introduced.

User Contributed Notes

  1. Skip to note 6 content

    accepted slugs:
    <br />
    'title'<br />
    'editor' (content)<br />
    'author'<br />
    'thumbnail' (featured image) (current theme must also support Post Thumbnails)<br />
    'excerpt'<br />
    'trackbacks'<br />
    'custom-fields' (see Custom_Fields, aka meta-data)<br />
    'comments' (also will see comment count balloon on edit screen)<br />
    'revisions' (will store revisions)<br />
    'page-attributes' (template and menu order) (hierarchical must be true)<br />
    'post-formats' (see Post_Formats)