前言
以编程方式修改应用于页面的模板(@since 1.8.4)。这是通过 bricks/screen_conditions/scores 筛选器设置激活模板的替代方式。在下例中,我们希望在 Bricks 数据(使用 Bricks 编辑)的单篇文章中排除使用模板。还有一种场景是自定义文章类型存在多个单篇模板。通过此筛选器,你可以基于自定义字段决定使用哪个模板。从 Bricks 1.12 起,应用于页面的模板也会显示在构建器中。之前只能看到 Post Content 元素,难以对周围元素进行样式化。如果你想保留旧行为,并仅在构建器内禁用已应用模板,可使用此筛选器:
用法
将代码加入子主题的 functions.php:
add_filter( 'bricks/active_templates', 'disable_template_in_builder', 10, 3 );
function disable_template_in_builder( $active_templates, $post_id, $content_type ) {
// Only run my logic in the Builder
if ( bricks_is_builder() ) {
$active_templates['content'] = 0;
}
return $active_templates;
}
示例代码
筛选返回值
add_filter( 'bricks/active_templates', 'disable_template_in_builder', 10, 3 );
function disable_template_in_builder( $active_templates, $post_id, $content_type ) {
// Only run my logic in the Builder
if ( bricks_is_builder() ) {
$active_templates['content'] = 0;
}
return $active_templates;
}
补充说明
更多细节可参考 官方文档。