前言
Bricks 1.4 引入。由于其改进且更精简的 DOM 结构,现在需要在 render() 函数中直接添加元素 ID、根类和其它元素根 HTML 属性。你可以通过以下方式以编程方式操作返回的元素根属性:筛选器回调接收 2 个参数:$attributes 和 $element_id。
用法
将代码加入子主题的 functions.php:
add_filter( 'bricks/element/builder_setup_query', function( $query_args, $post_id ) {
// Example: If editing a specific template, force a specific post for preview
if ( $post_id === 123 ) {
$query_args['p'] = 456;
$query_args['post_type'] = 'post';
}
return $query_args;
}, 10, 2 );
参数
$query_args(array):传递给 WP_Query 的参数数组。$post_id(int):正在编辑的模板 ID。
示例代码
筛选返回值
add_filter( 'bricks/element/builder_setup_query', function( $query_args, $post_id ) {
// Example: If editing a specific template, force a specific post for preview
if ( $post_id === 123 ) {
$query_args['p'] = 456;
$query_args['post_type'] = 'post';
}
return $query_args;
}, 10, 2 );
根据条件短路返回原值
根据条件跳过或修改返回值:
add_filter( 'bricks/element/builder_setup_query', function( $value, $query_args ) {
if ( is_admin() ) { return $value; }
// TODO: custom logic here
return $value;
} );
补充说明
更多细节可参考 官方文档。