前言
筛选 Bricks 为当前页面确定应渲染的激活模板数组(页头、内容、页脚、弹窗)。
用法
将代码加入子主题的 functions.php:
add_filter( 'bricks/active_templates', function( $active_templates, $post_id, $content_type ) {
// Example: Use a specific header template (ID: 1234) for single posts
if ( is_single() ) {
$active_templates['header'] = 1234;
}
return $active_templates;
}, 10, 3 );
参数
$active_templates(array):按类型分组的激活模板数组(如 header、footer、content、popup)。$post_id(int):正在被渲染的当前文章 ID。$content_type(string):正在被渲染的内容类型(例如 content、archive、search、error)。
示例代码
筛选返回值
add_filter( 'bricks/active_templates', function( $active_templates, $post_id, $content_type ) {
// Example: Use a specific header template (ID: 1234) for single posts
if ( is_single() ) {
$active_templates['header'] = 1234;
}
return $active_templates;
}, 10, 3 );
根据条件短路返回原值
根据条件跳过或修改返回值:
add_filter( 'bricks/active_templates', function( $value, $active_templates ) {
if ( is_admin() ) { return $value; }
// TODO: custom logic here
return $value;
} );
补充说明
更多细节可参考 官方文档。