前言
筛选 Bricks 构建器面板中默认展开的元素类别。允许你根据正在编辑的文章类型或 ID 优先展示特定元素类别。
用法
将代码加入子主题的 functions.php:
add_filter( 'bricks/builder/first_element_category', function( $category, $post_id, $post_type ) {
// Example: Expand 'woocommerce' category for product templates
if ( $post_type === 'product' ) {
return 'woocommerce';
}
return $category;
}, 10, 3 );
参数
$category(string|bool):默认展开的类别名称(例如 layout、basic、single)。默认为 false。$post_id(int):正在被编辑的文章 ID。$post_type(string):正在被编辑的文章的文章类型。
示例代码
筛选返回值
add_filter( 'bricks/builder/first_element_category', function( $category, $post_id, $post_type ) {
// Example: Expand 'woocommerce' category for product templates
if ( $post_type === 'product' ) {
return 'woocommerce';
}
return $category;
}, 10, 3 );
根据条件短路返回原值
根据条件跳过或修改返回值:
add_filter( 'bricks/builder/first_element_category', function( $value, $category ) {
if ( is_admin() ) { return $value; }
// TODO: custom logic here
return $value;
} );
补充说明
更多细节可参考 官方文档。