Bricks 中文文档:bricks/get_templates

📄 原始资料  |  展开查看原始 JSON(bricks-get_templates.json){ “name”: “bricks/get_templates”, “kind”: “filter”, “url”: “https://academy.bricksbuilder.io/developer/hooks/filters/bricks-get_templates/”, “scraped_at”: “2026-08-29”, “hook_name”: “bricks/get_templates”, “description”: { “en”: “Filters the…

📄 原始资料  |  展开查看原始 JSON(bricks-get_templates.json)
{
  "name": "bricks/get_templates",
  "kind": "filter",
  "url": "https://academy.bricksbuilder.io/developer/hooks/filters/bricks-get_templates/",
  "scraped_at": "2026-08-29",
  "hook_name": "bricks/get_templates",
  "description": {
    "en": "Filters the list of templates returned by the Templates::get_templates() method. This allows you to modify the templates array before it is returned to the builder or other parts of the application.",
    "zh": "筛选当前页面的主查询结果。"
  },
  "parameters": [
    {
      "name": "templates",
      "type": "array",
      "type_zh": null,
      "description": {
        "en": "Array of template data.",
        "zh": "模板数据数组。"
      }
    },
    {
      "name": "custom_args",
      "type": "array",
      "type_zh": null,
      "description": {
        "en": "The arguments used to query the templates.",
        "zh": "用于查询模板的参数。"
      }
    }
  ],
  "example_usage": "add_filter( 'bricks/get_templates', function( $templates, $custom_args ) {\n    // Example: Add a custom property to all templates\n    foreach ( $templates as $key => $template ) {\n        $templates[ $key ]['my_custom_property'] = 'value';\n    }\n\n\n    return $templates;\n}, 10, 2 );"
}

前言

筛选当前页面的主查询结果。

用法

将代码加入子主题的 functions.php

add_filter( 'bricks/get_templates', function( $templates, $custom_args ) {
    // Example: Add a custom property to all templates
    foreach ( $templates as $key => $template ) {
        $templates[ $key ]['my_custom_property'] = 'value';
    }


    return $templates;
}, 10, 2 );

参数

  • $templates (array):模板数据数组。
  • $custom_args (array):用于查询模板的参数。

示例代码

筛选返回值

add_filter( 'bricks/get_templates', function( $templates, $custom_args ) {
    // Example: Add a custom property to all templates
    foreach ( $templates as $key => $template ) {
        $templates[ $key ]['my_custom_property'] = 'value';
    }


    return $templates;
}, 10, 2 );

根据条件短路返回原值

根据条件跳过或修改返回值:

add_filter( 'bricks/get_templates', function( $value, $templates ) {
    if ( is_admin() ) { return $value; }
    // TODO: custom logic here
    return $value;
} );

补充说明

更多细节可参考 官方文档