_inject_theme_attribute_in_block_template_content

函数
_inject_theme_attribute_in_block_template_content ( $template_content )
Access
Private
参数
  • (string) $template_content serialized wp_template content.
    Required:
返回值
  • (string) Updated 'wp_template' content.
定义位置
相关方法
_remove_theme_attribute_in_block_template_contentget_default_block_template_typesget_the_block_template_htmlget_theme_starter_content_register_theme_block_patterns
引入
5.9.0
弃用
-

inject_theme_attribute_in_block_template_content是一个WordPress函数,用于将主题属性注入到块模板的内容中: 这是通过解析区块模板的HTML内容并根据其类型为某些元素添加属性来实现的。例如,如果区块模板包含一个图像元素,该函数可能会给该元素添加一个”data-theme-url”属性,其值指向当前主题的目录: 这个函数在区块编辑器中使用,允许主题提供自定义区块模板,包括主题特定的功能。

解析wp_template内容,并将活动主题的样式表作为主题属性注入每个wp_template_part中。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function _inject_theme_attribute_in_block_template_content( $template_content ) {
$has_updated_content = false;
$new_content = '';
$template_blocks = parse_blocks( $template_content );
$blocks = _flatten_blocks( $template_blocks );
foreach ( $blocks as &$block ) {
if (
'core/template-part' === $block['blockName'] &&
! isset( $block['attrs']['theme'] )
) {
$block['attrs']['theme'] = wp_get_theme()->get_stylesheet();
$has_updated_content = true;
}
}
if ( $has_updated_content ) {
foreach ( $template_blocks as &$block ) {
$new_content .= serialize_block( $block );
}
return $new_content;
}
return $template_content;
}
function _inject_theme_attribute_in_block_template_content( $template_content ) { $has_updated_content = false; $new_content = ''; $template_blocks = parse_blocks( $template_content ); $blocks = _flatten_blocks( $template_blocks ); foreach ( $blocks as &$block ) { if ( 'core/template-part' === $block['blockName'] && ! isset( $block['attrs']['theme'] ) ) { $block['attrs']['theme'] = wp_get_theme()->get_stylesheet(); $has_updated_content = true; } } if ( $has_updated_content ) { foreach ( $template_blocks as &$block ) { $new_content .= serialize_block( $block ); } return $new_content; } return $template_content; }
function _inject_theme_attribute_in_block_template_content( $template_content ) {
	$has_updated_content = false;
	$new_content         = '';
	$template_blocks     = parse_blocks( $template_content );

	$blocks = _flatten_blocks( $template_blocks );
	foreach ( $blocks as &$block ) {
		if (
			'core/template-part' === $block['blockName'] &&
			! isset( $block['attrs']['theme'] )
		) {
			$block['attrs']['theme'] = wp_get_theme()->get_stylesheet();
			$has_updated_content     = true;
		}
	}

	if ( $has_updated_content ) {
		foreach ( $template_blocks as &$block ) {
			$new_content .= serialize_block( $block );
		}

		return $new_content;
	}

	return $template_content;
}

常见问题

FAQs
查看更多 >