Bricks 中文文档:bricks/query/force_loop_index

标签TAGS: 

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

📄 原始资料  |  展开查看原始 JSON(bricks-query-force_loop_index.json)
{
  "name": "bricks/query/force_loop_index",
  "kind": "filter",
  "url": "https://academy.bricksbuilder.io/developer/hooks/filters/bricks-query-force_loop_index/",
  "scraped_at": "2026-08-29",
  "hook_name": "bricks/query/force_loop_index",
  "description": {
    "en": "Forces Bricks\\Query::get_loop_index() to return a specific value. This is useful for AJAX contexts (like popups) where you need to simulate a specific loop iteration to render correct data or styles.",
    "zh": "强制 Bricks\\Query::get_loop_index() 返回特定值。在 AJAX 上下文(如弹窗)中很有用,在这些上下文中你需要模拟特定循环迭代以渲染正确的数据或样式。"
  },
  "parameters": [
    {
      "name": "index",
      "type": "string|int",
      "type_zh": null,
      "description": {
        "en": "The forced loop index. Default is '' (empty string), meaning no override.",
        "zh": "被强制的循环索引。默认为 ''(空字符串),表示不覆盖。"
      }
    }
  ],
  "example_usage": "add_filter( 'bricks/query/force_loop_index', function( $index ) {\n    // Example: Force loop index to 0\n    return 0;\n} );"
}

前言

强制 Bricks\Query::get_loop_index() 返回特定值。在 AJAX 上下文(如弹窗)中很有用,在这些上下文中你需要模拟特定循环迭代以渲染正确的数据或样式。

用法

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

add_filter( 'bricks/query/force_loop_index', function( $index ) {
    // Example: Force loop index to 0
    return 0;
} );

参数

  • $index (string|int):被强制的循环索引。默认为 ”(空字符串),表示不覆盖。

示例代码

筛选返回值

add_filter( 'bricks/query/force_loop_index', function( $index ) {
    // Example: Force loop index to 0
    return 0;
} );

根据条件短路返回原值

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

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

补充说明

更多细节可参考 官方文档