函数文档

_add_block_template_part_area_info()

💡 云策文档标注

概述

_add_block_template_part_area_info() 函数用于向输入模板信息中添加模板部分的区域信息,基于主题的 theme.json 文件或默认值。

关键要点

  • 函数接受一个数组参数 $template_info,必须包含 'type' 和 'slug' 字段。
  • 如果主题有 theme.json 文件,则从 wp_get_theme_data_template_parts() 获取模板部分元数据。
  • 根据 $template_info['slug'] 在元数据中查找 'area',如果存在则添加 'title' 和过滤后的 'area',否则设置 'area' 为 WP_TEMPLATE_PART_AREA_UNCATEGORIZED。
  • 返回更新后的 $template_info 数组。

代码示例

function _add_block_template_part_area_info( $template_info ) {
	if ( wp_theme_has_theme_json() ) {
		$theme_data = wp_get_theme_data_template_parts();
	}

	if ( isset( $theme_data[ $template_info['slug'] ]['area'] ) ) {
		$template_info['title'] = $theme_data[ $template_info['slug'] ]['title'];
		$template_info['area']  = _filter_block_template_part_area( $theme_data[ $template_info['slug'] ]['area'] );
	} else {
		$template_info['area'] = WP_TEMPLATE_PART_AREA_UNCATEGORIZED;
	}

	return $template_info;
}

注意事项

  • 函数内部依赖 wp_theme_has_theme_json() 和 wp_get_theme_data_template_parts() 来检查和处理主题数据。
  • 使用 _filter_block_template_part_area() 函数来验证和过滤区域值。
  • 如果未找到区域信息,默认使用 WP_TEMPLATE_PART_AREA_UNCATEGORIZED 常量。
  • 此函数在 WordPress 5.9.0 版本中引入。

📄 原文内容

Attempts to add the template part’s area information to the input template.

Parameters

$template_infoarrayrequired
Template to add information to (requires 'type' and 'slug' fields).

Return

array Template info.

Source

function _add_block_template_part_area_info( $template_info ) {
	if ( wp_theme_has_theme_json() ) {
		$theme_data = wp_get_theme_data_template_parts();
	}

	if ( isset( $theme_data[ $template_info['slug'] ]['area'] ) ) {
		$template_info['title'] = $theme_data[ $template_info['slug'] ]['title'];
		$template_info['area']  = _filter_block_template_part_area( $theme_data[ $template_info['slug'] ]['area'] );
	} else {
		$template_info['area'] = WP_TEMPLATE_PART_AREA_UNCATEGORIZED;
	}

	return $template_info;
}

Changelog

Version Description
5.9.0 Introduced.