钩子文档

get_block_templates

💡 云策文档标注

概述

get_block_templates 是一个 WordPress 过滤器,用于在获取块模板数组后对其进行修改。它允许开发者基于查询参数调整查询结果。

关键要点

  • 过滤器名称:get_block_templates
  • 参数:$query_result(WP_Block_Template[] 数组)、$query(查询参数数组)、$template_type(字符串,如 'wp_template' 或 'wp_template_part')
  • 用途:过滤或修改已获取的块模板数组,常用于自定义模板查询逻辑

注意事项

  • 查询参数 $query 包括 slug__in、wp_id、area 和 post_type,均为可选
  • area 参数仅适用于 'wp_template_part' 模板类型,基于 'wp_template_part_area' 分类法
  • 此过滤器从 WordPress 5.9.0 版本开始引入

📄 原文内容

Filters the array of queried block templates array after they’ve been fetched.

Parameters

$query_resultWP_Block_Template[]
Array of found block templates.
$queryarray
Arguments to retrieve templates. All arguments are optional.

  • slug__in string[]
    List of slugs to include.
  • wp_id int
    Post ID of customized template.
  • area string
    A 'wp_template_part_area' taxonomy value to filter by (for 'wp_template_part' template type only).
  • post_type string
    Post type to get the templates for.

$template_typestring
wp_template or wp_template_part.

Source

return apply_filters( 'get_block_templates', $query_result, $query, $template_type );

Changelog

Version Description
5.9.0 Introduced.