Bricks 中文文档:bricks/query/force_is_looping

标签TAGS: 

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

📄 原始资料  |  展开查看原始 JSON(bricks-query-force_is_looping.json)
{
  "name": "bricks/query/force_is_looping",
  "kind": "filter",
  "url": "https://academy.bricksbuilder.io/developer/hooks/filters/bricks-query-force_is_looping/",
  "scraped_at": "2026-08-29",
  "hook_name": "bricks/query/force_is_looping",
  "description": {
    "en": "Forces Bricks\\Query::is_looping() to return true . This is useful in AJAX contexts (like popups) where you need to simulate being inside a query loop to correctly render dynamic data that depends on the loop context.",
    "zh": "强制 Bricks\\Query::is_looping() 返回 true。在 AJAX 上下文(如弹窗)中很有用,在这些上下文中你需要模拟处于查询循环中以正确渲染依赖于循环上下文的动态数据。"
  },
  "parameters": [
    {
      "name": "force",
      "type": "bool",
      "type_zh": null,
      "description": {
        "en": "Whether to force the is_looping state. Default is false .",
        "zh": "是否强制 is_looping 状态。默认为 false。"
      }
    },
    {
      "name": "query_id",
      "type": "string",
      "type_zh": null,
      "description": {
        "en": "The ID of the query being checked.",
        "zh": "正在被检查的查询 ID。"
      }
    },
    {
      "name": "element_id",
      "type": "string",
      "type_zh": null,
      "description": {
        "en": "The ID of the element being checked.",
        "zh": "正在被检查的元素 ID。"
      }
    }
  ],
  "example_usage": "add_filter( 'bricks/query/force_is_looping', function( $force, $query_id, $element_id ) {\n    // Example: Force looping for a specific element ID\n    if ( $element_id === 'my_element_id' ) {\n        return true;\n    }\n\n\n    return $force;\n}, 10, 3 );"
}

前言

强制 Bricks\Query::is_looping() 返回 true。在 AJAX 上下文(如弹窗)中很有用,在这些上下文中你需要模拟处于查询循环中以正确渲染依赖于循环上下文的动态数据。

用法

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

add_filter( 'bricks/query/force_is_looping', function( $force, $query_id, $element_id ) {
    // Example: Force looping for a specific element ID
    if ( $element_id === 'my_element_id' ) {
        return true;
    }


    return $force;
}, 10, 3 );

参数

  • $force (bool):是否强制 is_looping 状态。默认为 false。
  • $query_id (string):正在被检查的查询 ID。
  • $element_id (string):正在被检查的元素 ID。

示例代码

筛选返回值

add_filter( 'bricks/query/force_is_looping', function( $force, $query_id, $element_id ) {
    // Example: Force looping for a specific element ID
    if ( $element_id === 'my_element_id' ) {
        return true;
    }


    return $force;
}, 10, 3 );

根据条件短路返回原值

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

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

补充说明

更多细节可参考 官方文档