前言
筛选为外部 API 查询循环计算的总页数。当 API 响应的分页结构与 Bricks 的标准提取逻辑不匹配时,这很有用。
用法
将代码加入子主题的 functions.php:
add_filter( 'bricks/query_api/total_pages', function( $total_pages, $element_id, $query_api ) {
// Example: Set total pages for a specific API query
if ( $element_id === 'my_api_loop' ) {
// Assume you stored the total in a custom property or need a fixed value
return 10;
}
return $total_pages;
}, 10, 3 );
参数
$total_pages(int):已计算的总页数。$element_id(string):查询元素的 ID。$query_api(object):Bricks\Integrations\Query\Query_API 实例。
示例代码
筛选返回值
add_filter( 'bricks/query_api/total_pages', function( $total_pages, $element_id, $query_api ) {
// Example: Set total pages for a specific API query
if ( $element_id === 'my_api_loop' ) {
// Assume you stored the total in a custom property or need a fixed value
return 10;
}
return $total_pages;
}, 10, 3 );
根据条件短路返回原值
根据条件跳过或修改返回值:
add_filter( 'bricks/query_api/total_pages', function( $value, $total_pages ) {
if ( is_admin() ) { return $value; }
// TODO: custom logic here
return $value;
} );
补充说明
更多细节可参考 官方文档。