函数文档

_get_block_template_file()

💡 云策文档标注

概述

_get_block_template_file() 函数用于根据给定的模板类型和 slug 从主题中检索模板文件。它返回包含模板元数据的数组或 null,适用于块主题中的 'wp_template' 或 'wp_template_part' 类型。

关键要点

  • 参数:$template_type(必需,字符串,只能是 'wp_template' 或 'wp_template_part')和 $slug(必需,字符串,模板 slug)。
  • 返回值:数组(包含 slug、path、theme、type 等元数据)或 null(如果 $template_type 无效或文件不存在)。
  • 功能:遍历活动主题和父主题目录,查找匹配的 .html 文件,并调用辅助函数添加额外信息。
  • 依赖函数:如 get_block_theme_folders()、_add_block_template_part_area_info() 和 _add_block_template_info()。

代码示例

function _get_block_template_file( $template_type, $slug ) {
    if ( 'wp_template' !== $template_type && 'wp_template_part' !== $template_type ) {
        return null;
    }

    $themes = array(
        get_stylesheet() => get_stylesheet_directory(),
        get_template()   => get_template_directory(),
    );
    foreach ( $themes as $theme_slug => $theme_dir ) {
        $template_base_paths = get_block_theme_folders( $theme_slug );
        $file_path           = $theme_dir . '/' . $template_base_paths[ $template_type ] . '/' . $slug . '.html';
        if ( file_exists( $file_path ) ) {
            $new_template_item = array(
                'slug'  => $slug,
                'path'  => $file_path,
                'theme' => $theme_slug,
                'type'  => $template_type,
            );

            if ( 'wp_template_part' === $template_type ) {
                return _add_block_template_part_area_info( $new_template_item );
            }

            // If it's not a `wp_template_part`, it must be a `wp_template`.
            return _add_block_template_info( $new_template_item );
        }
    }

    return null;
}

📄 原文内容

Retrieves the template file from the theme for a given slug.

Parameters

$template_typestringrequired
Template type. Either 'wp_template' or 'wp_template_part'.
$slugstringrequired
Template slug.

Return

array|null Array with template metadata if $template_type is one of ‘wp_template’ or ‘wp_template_part’, null otherwise.

  • slug string
    Template slug.
  • path string
    Template file path.
  • theme string
    Theme slug.
  • type string
    Template type.
  • area string
    Template area. Only for 'wp_template_part'.
  • title string
    Optional. Template title.
  • postTypes string[]
    Optional. List of post types that the template supports. Only for 'wp_template'.

Source

function _get_block_template_file( $template_type, $slug ) {
	if ( 'wp_template' !== $template_type && 'wp_template_part' !== $template_type ) {
		return null;
	}

	$themes = array(
		get_stylesheet() => get_stylesheet_directory(),
		get_template()   => get_template_directory(),
	);
	foreach ( $themes as $theme_slug => $theme_dir ) {
		$template_base_paths = get_block_theme_folders( $theme_slug );
		$file_path           = $theme_dir . '/' . $template_base_paths[ $template_type ] . '/' . $slug . '.html';
		if ( file_exists( $file_path ) ) {
			$new_template_item = array(
				'slug'  => $slug,
				'path'  => $file_path,
				'theme' => $theme_slug,
				'type'  => $template_type,
			);

			if ( 'wp_template_part' === $template_type ) {
				return _add_block_template_part_area_info( $new_template_item );
			}

			// If it's not a `wp_template_part`, it must be a `wp_template`.
			return _add_block_template_info( $new_template_item );
		}
	}

	return null;
}

Changelog

Version Description
5.9.0 Introduced.