函数文档

get_theme_support()

💡 云策文档标注

概述

get_theme_support() 函数用于获取主题支持功能的注册参数,是 WordPress 主题开发中检查 add_theme_support() 设置的核心工具。它支持多种功能参数,如 custom-logo、custom-header 等,并可返回具体参数值。

关键要点

  • 函数用于检索主题支持功能的参数,基于 add_theme_support() 的注册信息。
  • 参数 $feature 为必需,指定要检查的功能,如 'custom-logo'、'custom-header' 等,支持列表包括 admin-bar、align-wide 等核心值。
  • 可选参数 $args 用于针对特定功能检查额外参数,例如 get_theme_support('custom-header', 'width') 可获取宽度值。
  • 返回值类型为 mixed,可能返回参数数组、具体值或 false(如果功能未注册)。
  • 内部实现基于全局变量 $_wp_theme_features,并针对 custom-logo、custom-header、custom-background 等特殊功能有特定处理逻辑。
  • 函数在 WordPress 3.1.0 中引入,5.3.0 版本正式化 ...$args 参数签名。

代码示例

// 获取 custom-logo 支持参数
$theme_support = get_theme_support( 'custom-logo' );

// 获取 custom-header 的宽度参数
$width = get_theme_support( 'custom-header', 'width' );

// 获取 custom-background 支持参数示例
$theme_support = get_theme_support( 'custom-background' );
// 输出可能为包含 default-image、default-color 等键的数组

注意事项

  • 使用前需确保主题已通过 add_theme_support() 注册相应功能,否则返回 false。
  • 对于 custom-logo、custom-header、custom-background 等功能,可通过 $args 参数获取特定子参数值。
  • 函数与多个核心功能相关,如 get_custom_logo()、is_header_video_active() 等,常用于主题定制和插件开发中。

📄 原文内容

Gets the theme support arguments passed when registering that support.

Description

Example usage:

get_theme_support( 'custom-logo' );
get_theme_support( 'custom-header', 'width' );

Parameters

$featurestringrequired
The feature to check. 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'

$argsmixedoptional
Optional extra arguments to be checked against certain features.

Return

mixed The array of extra arguments or the value for the registered feature.

Source

function get_theme_support( $feature, ...$args ) {
	global $_wp_theme_features;

	if ( ! isset( $_wp_theme_features[ $feature ] ) ) {
		return false;
	}

	if ( ! $args ) {
		return $_wp_theme_features[ $feature ];
	}

	switch ( $feature ) {
		case 'custom-logo':
		case 'custom-header':
		case 'custom-background':
			if ( isset( $_wp_theme_features[ $feature ][0][ $args[0] ] ) ) {
				return $_wp_theme_features[ $feature ][0][ $args[0] ];
			}
			return false;

		default:
			return $_wp_theme_features[ $feature ];
	}
}

Changelog

Version Description
5.3.0 Formalized the existing and already documented ...$args parameter by adding it to the function signature.
3.1.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Gets the `custom-background` theme support arguments

    $theme_support = get_theme_support( 'custom-background' );

    Output:

    Array
    (
        [0] => Array
            (
                [default-image] =>
                [default-repeat] => repeat
                [default-position-x] => left
                [default-attachment] => scroll
                [default-color] => ffffff
                [wp-head-callback] => _custom_background_cb
                [admin-head-callback] =>
                [admin-preview-callback] =>
            )
    
    )