remove_theme_support()
云策文档标注
概述
remove_theme_support() 函数用于在主题中取消对特定功能的支持,通常在子主题中用于覆盖父主题的功能设置。它应在主题的 functions.php 文件中调用。
关键要点
- 函数用于取消主题对特定功能的支持,如 'post-thumbnails'、'custom-logo' 等。
- 主要应用于子主题,以移除或修改从父主题继承的功能支持。
- 参数 $feature 是必需的字符串,指定要移除的功能,参考 add_theme_support() 的列表。
- 返回值为布尔值或 void,表示功能是否被成功移除。
- 内部函数 _remove_theme_support() 不应直接使用,因为它会忽略某些主题不直接使用的内部注册。
代码示例
// 在子主题的 functions.php 中移除对页面特色图像的支持
add_action( 'after_setup_theme', 'remove_featured_images_from_child_theme', 11 );
function remove_featured_images_from_child_theme() {
remove_theme_support( 'post-thumbnails' );
add_theme_support( 'post-thumbnails', array( 'post' ) );
}注意事项
- 调用时机:建议在 after_setup_theme 钩子中使用,优先级设为 11 以确保在父主题(默认优先级 10)之后执行。
- 避免移除内部注册:某些功能如 'editor-style'、'widgets'、'menus' 不应被移除,函数会返回 false。
- 相关函数:参考 add_theme_support() 来添加功能支持,以及已弃用的 remove_custom_image_header() 和 remove_custom_background()。
原文内容
Allows a theme to de-register its support of a certain feature
Description
Should be called in the theme’s functions.php file. Generally would be used for child themes to override support from the parent theme.
See also
Parameters
$featurestringrequired-
The feature being removed. See add_theme_support() for the list of possible values.
More Arguments from add_theme_support( … $feature )
The feature being added. Likely core values include:
'admin-bar''align-wide''appearance-tools''automatic-feed-links''block-templates''block-template-parts''border''core-block-patterns''custom-background''custom-header''custom-line-height''custom-logo''customize-selective-refresh-widgets''custom-spacing''custom-units''dark-editor-style''disable-custom-colors''disable-custom-font-sizes''disable-custom-gradients''disable-layout-styles''editor-color-palette''editor-gradient-presets''editor-font-sizes''editor-spacing-sizes''editor-styles''featured-content''html5''link-color''menus''post-formats''post-thumbnails''responsive-embeds''starter-content''title-tag''widgets''widgets-block-editor''wp-block-styles'
Source
function remove_theme_support( $feature ) {
// Do not remove internal registrations that are not used directly by themes.
if ( in_array( $feature, array( 'editor-style', 'widgets', 'menus' ), true ) ) {
return false;
}
return _remove_theme_support( $feature );
}
Changelog
| Version | Description |
|---|---|
| 3.0.0 | Introduced. |
Skip to note 2 content
Codex
Removing a Feature In a Child Theme
In some cases, a Parent Theme may have activated a feature that you do not want to have available in your Child Theme. For instance, if you are using a parent theme that has activated Featured Images for all Pages and Posts, but you’d like to remove the functionality of having Featured Images for Pages in your Child Theme, you could do something like this:
// in your Child Theme's functions.php // Use the after_setup_theme hook with a priority of 11 to load after the // parent theme, which will fire on the default priority of 10 add_action( 'after_setup_theme', 'remove_featured_images_from_child_theme', 11 ); function remove_featured_images_from_child_theme() { // This will remove support for post thumbnails on ALL Post Types remove_theme_support( 'post-thumbnails' ); // Add this line in to re-enable support for just Posts add_theme_support( 'post-thumbnails', array( 'post' ) ); }