函数文档

_inject_theme_attribute_in_template_part_block()

💡 云策文档标注

概述

_inject_theme_attribute_in_template_part_block() 函数用于将当前活动主题的样式表名称作为 theme 属性注入到指定的模板部分块中,确保块在渲染时能正确关联主题。

关键要点

  • 函数检查块是否为 'core/template-part' 类型,且未设置 theme 属性时,自动注入 get_stylesheet() 返回值。
  • 参数 $block 是必需的,为一个已解析的块数组,通过引用传递以直接修改。
  • 此函数自 WordPress 6.4.0 版本引入,常用于块编辑器上下文以增强主题兼容性。

代码示例

function _inject_theme_attribute_in_template_part_block( &$block ) {
    if (
        'core/template-part' === $block['blockName'] &&
        ! isset( $block['attrs']['theme'] )
    ) {
        $block['attrs']['theme'] = get_stylesheet();
    }
}

📄 原文内容

Injects the active theme’s stylesheet as a theme attribute into a given template part block.

Parameters

$blockarrayrequired
a parsed block.

Source

function _inject_theme_attribute_in_template_part_block( &$block ) {
	if (
		'core/template-part' === $block['blockName'] &&
		! isset( $block['attrs']['theme'] )
	) {
		$block['attrs']['theme'] = get_stylesheet();
	}
}

Changelog

Version Description
6.4.0 Introduced.