add_post_type_support()
概述
add_post_type_support() 函数用于为文章类型注册特定功能支持,如标题、编辑器、评论等核心功能。它通过全局变量 $_wp_post_type_features 存储支持状态,并可传递额外参数。
关键要点
- 功能:注册文章类型对特定功能的支持,包括 'title'、'editor'、'comments'、'revisions'、'trackbacks'、'author'、'excerpt'、'page-attributes'、'thumbnail'、'custom-fields' 和 'post-formats'。
- 参数:$post_type(文章类型字符串,必需)、$feature(功能字符串或数组,必需)、$args(可选额外参数)。
- 用法:应在 init action hook 中调用,支持单个功能或功能数组,并可传递参数。
- 注意事项:'thumbnail' 功能需配合 add_theme_support('post-thumbnails') 使用;多站点中需配置上传设置以显示“特色图片”元框。
代码示例
add_post_type_support( 'my_post_type', 'comments' );
add_post_type_support( 'my_post_type', array(
'author', 'excerpt',
) );
add_post_type_support( 'my_post_type', 'my_feature', array(
'field' => 'value',
) );注意事项
- 调用时机:建议在 init action hook 中调用以确保正确初始化。
- 功能依赖:某些功能如 'thumbnail' 需要主题支持,需额外调用 add_theme_support()。
- 多站点配置:在多站点安装中,需在 Network Admin 的 Upload Settings 中启用媒体上传按钮以显示“特色图片”元框。
Registers support of certain features for a post type.
Description
All core features are directly associated with a functional area of the edit screen, such as the editor or a meta box. Features include: ‘title’, ‘editor’, ‘comments’, ‘revisions’, ‘trackbacks’, ‘author’, ‘excerpt’, ‘page-attributes’, ‘thumbnail’, ‘custom-fields’, and ‘post-formats’.
Additionally, the ‘revisions’ feature dictates whether the post type will store revisions, the ‘autosave’ feature dictates whether the post type will be autosaved, and the ‘comments’ feature dictates whether the comments count will show on the edit screen.
A third, optional parameter can also be passed along with a feature to provide additional information about supporting that feature.
Example usage:
add_post_type_support( 'my_post_type', 'comments' );
add_post_type_support( 'my_post_type', array(
'author', 'excerpt',
) );
add_post_type_support( 'my_post_type', 'my_feature', array(
'field' => 'value',
) );
Parameters
$post_typestringrequired-
The post type for which to add the feature.
$featurestring|arrayrequired-
The feature being added, accepts an array of feature strings or a single string.
$argsmixedoptional-
Optional extra arguments to pass along with certain features.
Source
function add_post_type_support( $post_type, $feature, ...$args ) {
global $_wp_post_type_features;
$features = (array) $feature;
foreach ( $features as $feature ) {
if ( $args ) {
$_wp_post_type_features[ $post_type ][ $feature ] = $args;
} else {
$_wp_post_type_features[ $post_type ][ $feature ] = true;
}
}
}
Skip to note 6 content
Hay
For an overview of all possible features (e.g. ‘title’, ‘editor’, etc.), see the documentation for
post_type_supports.Skip to note 7 content
Marc Heatley
Unfortunately,
add_post_type_support('page', 'thumbnail');won’t add featured images to pages. For that you need to [add theme support for post-thumbnails
add_theme_support( 'post-thumbnails', array( 'post', 'page' ) );https://developer.wordpress.org/reference/functions/add_theme_support/#post-thumbnails
Skip to note 8 content
Codex
This example adds support for excerpts in pages (assuming it is *not* showing under “Screen Options”):
Skip to note 9 content
Darshit Rajyaguru
The example that allows you to add support for specific feature to a custom post type. It’s used to enable or disable certain features, Such as custom fields, excerpts, comments, thumbnails, revisions and more for particular post type.
add_action( 'init', 'custom_post_type_support' ); function custom_post_type_support() { add_post_type_support( 'book', array( 'custom-fields', 'thumbnail' ) ); }Skip to note 10 content
Irfan
To enable `add_post_type_support` for a specific page or post, we can narrow down with condition.
Example: To add support for excerpt in page (id : 123), use this:
global $pagenow; if( ($pagenow == 'post.php') && (isset($_GET['post'])) ){ $page_id = 123; // set the "page id" to enable support if($_GET['post'] == $page_id){ add_post_type_support('page','excerpt'); } }Note: `$_GET[‘post’]` gives the current page id when we open page edit screen.