函数文档

wp_enable_block_templates()

💡 云策文档标注

概述

wp_enable_block_templates() 函数用于为支持 theme.json 的主题默认启用块模板(编辑器模式)。它通过检查主题是否为块主题或包含 theme.json 文件,自动添加 block-templates 主题支持。

关键要点

  • 函数自动检测主题是否支持块模板:使用 wp_is_block_theme() 检查是否为块主题,或 wp_theme_has_theme_json() 检查是否有 theme.json 文件。
  • 当条件满足时,调用 add_theme_support('block-templates') 启用块模板功能。
  • 此函数从 WordPress 5.8.0 版本引入,适用于主题开发中集成块编辑器功能。

代码示例

function wp_enable_block_templates() {
    if ( wp_is_block_theme() || wp_theme_has_theme_json() ) {
        add_theme_support( 'block-templates' );
    }
}

📄 原文内容

Enables the block templates (editor mode) for themes with theme.json by default.

Source

function wp_enable_block_templates() {
	if ( wp_is_block_theme() || wp_theme_has_theme_json() ) {
		add_theme_support( 'block-templates' );
	}
}

Changelog

Version Description
5.8.0 Introduced.