函数文档

require_if_theme_supports()

💡 云策文档标注

概述

require_if_theme_supports() 是一个 WordPress 函数,用于在加载实现特定功能的文件之前检查当前主题是否支持该功能。它基于主题支持状态有条件地包含文件,并返回布尔值指示操作结果。

关键要点

  • 函数接受两个参数:$feature(字符串,必需)指定要检查的功能,如 'custom-logo' 或 'post-thumbnails';$file(字符串,必需)指定要加载的文件路径。
  • 如果当前主题支持 $feature,则加载 $file 并返回 true;否则返回 false。
  • 功能列表参考 add_theme_support(),包括核心值如 'admin-bar'、'custom-logo'、'post-formats' 等。
  • 相关函数:current_theme_supports() 用于检查主题支持,无需加载文件。
  • 自 WordPress 2.9.0 版本引入。

代码示例

function require_if_theme_supports( $feature, $file ) {
	if ( current_theme_supports( $feature ) ) {
		require $file;
		return true;
	}
	return false;
}

📄 原文内容

Checks a theme’s support for a given feature before loading the functions which implement it.

Parameters

$featurestringrequired
The feature being checked. 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'

$filestringrequired
Path to the file.

Return

bool True if the active theme supports the supplied feature, false otherwise.

Source

function require_if_theme_supports( $feature, $file ) {
	if ( current_theme_supports( $feature ) ) {
		require $file;
		return true;
	}
	return false;
}

Changelog

Version Description
2.9.0 Introduced.