pre_get_block_templates
云策文档标注
概述
pre_get_block_templates 是一个 WordPress 过滤器,用于在查询块模板数组之前拦截和修改查询过程。开发者可以通过此过滤器返回自定义的块模板数组来绕过默认的 WordPress 查询。
关键要点
- 过滤器名称:pre_get_block_templates
- 作用时机:在查询块模板数组之前执行
- 返回值:返回非 null 值(如 WP_Block_Template[] 数组)可短路默认查询,返回 null 则允许 WordPress 执行正常查询
- 参数:$block_templates(初始为 null)、$query(查询参数数组)、$template_type(模板类型,如 'wp_template' 或 'wp_template_part')
- 引入版本:WordPress 5.9.0
代码示例
$templates = apply_filters( 'pre_get_block_templates', null, $query, $template_type );注意事项
- $query 参数包含可选参数如 slug__in、wp_id、area(仅用于 'wp_template_part' 类型)、post_type 等,用于过滤模板查询。
- 此过滤器与 get_block_templates() 函数相关,用于检索基于查询的统一模板对象。
原文内容
Filters the block templates array before the query takes place.
Description
Return a non-null value to bypass the WordPress queries.
Parameters
$block_templatesWP_Block_Template[]|null-
Return an array of block templates to short-circuit the default query, or null to allow WP to run its normal queries.
$queryarray-
Arguments to retrieve templates. All arguments are optional.
slug__instring[]List of slugs to include.wp_idintPost ID of customized template.areastringA'wp_template_part_area'taxonomy value to filter by (for'wp_template_part'template type only).post_typestringPost type to get the templates for.
$template_typestring-
Template type. Either
'wp_template'or'wp_template_part'.
Source
$templates = apply_filters( 'pre_get_block_templates', null, $query, $template_type );
Changelog
| Version | Description |
|---|---|
| 5.9.0 | Introduced. |