前言
筛选 Bricks 用于检索页面数据(页头、内容、页脚)的文章 ID。允许你以编程方式更改当前页面请求中 Bricks 数据的来源。
用法
将代码加入子主题的 functions.php:
add_filter( 'bricks/builder/data_post_id', function( $post_id ) {
// Example: Use a specific post ID for a custom route
if ( get_query_var( 'my_custom_route' ) ) {
return 123; // ID of the post/template to use
}
return $post_id;
} );
参数
$post_id(int):当前正在编辑的文章 ID。
示例代码
筛选返回值
add_filter( 'bricks/builder/data_post_id', function( $post_id ) {
// Example: Use a specific post ID for a custom route
if ( get_query_var( 'my_custom_route' ) ) {
return 123; // ID of the post/template to use
}
return $post_id;
} );
根据条件短路返回原值
根据条件跳过或修改返回值:
add_filter( 'bricks/builder/data_post_id', function( $value, $post_id ) {
if ( is_admin() ) { return $value; }
// TODO: custom logic here
return $value;
} );
补充说明
更多细节可参考 官方文档。