函数文档

get_block_template()

💡 云策文档标注

概述

get_block_template() 函数用于根据唯一标识符检索单个统一的模板对象。它支持通过过滤器钩子进行查询前和查询后的自定义处理,并优先从数据库查询,若未找到则尝试从文件模板获取。

关键要点

  • 函数接受两个参数:$id(必需,模板唯一标识符,如 'theme_slug//template_slug')和 $template_type(可选,默认为 'wp_template',可选值包括 'wp_template' 或 'wp_template_part')。
  • 返回类型为 WP_Block_Template 或 null,表示找到的模板对象或空值。
  • 内部实现包括:首先应用 'pre_get_block_template' 过滤器以允许短路查询;然后解析 $id 并构建 WP_Query 从数据库检索;若未找到,则调用 get_block_file_template() 从文件获取;最后应用 'get_block_template' 过滤器返回结果。
  • 相关函数包括 get_block_file_template()、_build_block_template_result_from_post() 和 WP_Query 等,用于辅助模板检索和构建。
  • 该函数自 WordPress 5.8.0 版本引入,常用于 REST API 控制器和模板处理场景。

代码示例

// 示例:检索一个模板对象
$template = get_block_template( 'twentytwentyone//single-post', 'wp_template' );
if ( $template ) {
    // 处理模板对象
    echo $template->title;
} else {
    echo '模板未找到';
}

注意事项

  • 确保 $id 参数格式正确,使用 'theme_slug//template_slug' 形式,否则可能导致查询失败。
  • 利用 'pre_get_block_template' 和 'get_block_template' 过滤器可以自定义模板检索逻辑,例如缓存或外部数据源集成。
  • 函数内部处理了多种 post_status(如 'auto-draft'、'draft'、'publish'、'trash'),确保能检索到不同状态的模板。

📄 原文内容

Retrieves a single unified template object using its id.

Parameters

$idstringrequired
Template unique identifier (example: 'theme_slug//template_slug').
$template_typestringoptional
Template type. Either 'wp_template' or 'wp_template_part'.
Default 'wp_template'.

Return

WP_Block_Template|null Template.

Source

function get_block_template( $id, $template_type = 'wp_template' ) {
	/**
	 * Filters the block template object before the query takes place.
	 *
	 * Return a non-null value to bypass the WordPress queries.
	 *
	 * @since 5.9.0
	 *
	 * @param WP_Block_Template|null $block_template Return block template object to short-circuit the default query,
	 *                                               or null to allow WP to run its normal queries.
	 * @param string                 $id             Template unique identifier (example: 'theme_slug//template_slug').
	 * @param string                 $template_type  Template type. Either 'wp_template' or 'wp_template_part'.
	 */
	$block_template = apply_filters( 'pre_get_block_template', null, $id, $template_type );
	if ( ! is_null( $block_template ) ) {
		return $block_template;
	}

	$parts = explode( '//', $id, 2 );
	if ( count( $parts ) < 2 ) {
		return null;
	}
	list( $theme, $slug ) = $parts;
	$wp_query_args        = array(
		'post_name__in'  => array( $slug ),
		'post_type'      => $template_type,
		'post_status'    => array( 'auto-draft', 'draft', 'publish', 'trash' ),
		'posts_per_page' => 1,
		'no_found_rows'  => true,
		'tax_query'      => array(
			array(
				'taxonomy' => 'wp_theme',
				'field'    => 'name',
				'terms'    => $theme,
			),
		),
	);
	$template_query       = new WP_Query( $wp_query_args );
	$posts                = $template_query->posts;

	if ( count( $posts ) > 0 ) {
		$template = _build_block_template_result_from_post( $posts[0] );

		if ( ! is_wp_error( $template ) ) {
			return $template;
		}
	}

	$block_template = get_block_file_template( $id, $template_type );

	/**
	 * Filters the queried block template object after it's been fetched.
	 *
	 * @since 5.9.0
	 *
	 * @param WP_Block_Template|null $block_template The found block template, or null if there isn't one.
	 * @param string                 $id             Template unique identifier (example: 'theme_slug//template_slug').
	 * @param string                 $template_type  Template type. Either 'wp_template' or 'wp_template_part'.
	 */
	return apply_filters( 'get_block_template', $block_template, $id, $template_type );
}

Hooks

apply_filters( ‘get_block_template’, WP_Block_Template|null $block_template, string $id, string $template_type )

Filters the queried block template object after it’s been fetched.

apply_filters( ‘pre_get_block_template’, WP_Block_Template|null $block_template, string $id, string $template_type )

Filters the block template object before the query takes place.

Changelog

Version Description
5.8.0 Introduced.