当时版本的 Bricks 还没有内置的条件(Conditions)功能——但这不代表不能用自定义代码手动实现。
这篇教程提供几个实用示例,演示如何在 Bricks 站点里让任意元素只在指定条件满足时输出。
总体流程:
- 创建一个类型为「Section」的模板,把要条件显示的内容放进去;
- 在想要显示条件内容的位置添加 Code 元素,用 if 条件包住
do_shortcode()函数; - 如果条件基于自定义函数,记得把 if 条件包在
function_exists()检查里(防止函数未定义时报错)。
这个方案的小缺点:条件不满足时,HTML 输出里会残留一个空的 div。
这些示例大多是在本站搭建过程中整理出来的。
注:测试环境为 Bricks 1.4 RC1。
登录用户与访客显示不同内容
登录用户看到:
未登录访客看到:
Step 1
创建一个类型为 Section 的模板,命名为 “Logged In”(登录)。
把要展示给登录用户的内容放进 Section 里的 Container 中(提示:点 + 之后,点 Container 元素左上角的小 Layout 图标可快速切换容器布局):
Step 2
进入模板列表页面,把鼠标悬停在该模板上,通过浏览器状态栏 URL 记下它的 ID;或者把它的短代码复制到记事本。
在你想让登录用户看到内容的地方(页面/模板)添加一个 Code 元素,内容为:
<?php
if ( function_exists( 'is_user_logged_in' ) && is_user_logged_in() ) {
echo do_shortcode( '[bricks_template id="2485"]' );
}
?>
上面代码中把 2485 换成你自己的模板 ID。未登录用户在前端什么都看不到。
如果你想在未登录时显示另一个模板的内容,接着做 Step 3。
Step 3
再创建一个类型为 Section 的模板,命名为 “Not Logged In”(未登录)。
把未登录用户要看到的内容放进 Section 里的 Container 中:
把代码改成:
<?php
if ( function_exists( 'is_user_logged_in' ) && is_user_logged_in() ) {
echo do_shortcode( '[bricks_template id="2485"]' ); // 登录用户模板 ID
} else {
echo do_shortcode( '[bricks_template id="2486"]' ); // 未登录模板 ID
}
?>
按实际情况替换两个模板 ID:第一个短代码在用户登录时运行,第二个在未登录时运行。
检查循环中的文章是否属于某个分类
本站的 posts 网格里,被归类为 “Pro” 的文章会显示一个 “Pro” 丝带(ribbon)。逻辑代码如下:
Step 1
在子主题的 functions.php 中添加:
/**
* Function to check if the current post is assigned to Pro category
* @return string "Pro" if in the specified category
*/
function bl_is_in_pro_category() {
return in_category( 'pro' ) ? 'Pro' : '';
}
Step 2
在 Posts Grid 的 Section 模板里,丝带标题所在的 Code 元素中调用:
<?php echo bl_is_in_pro_category(); ?>
这样丝带就只会出现在标记为 Pro 的文章上。
MemberPress:会员与非会员显示不同内容
MemberPress 是本站选用的会员插件。
自定义函数:
/**
* Function to check if the current user has access to the membership.
*
* @return bool True if the user has access, false if not.
*/
function bl_user_has_membership() {
return current_user_can( 'mepr-active', 'memberships:51,367' );
}
其中 51 和 367 是要检查的会员 ID(membership ID)。
给已授权会员输出指定模板的 Code 元素示例:
<?php
if ( function_exists( 'bl_user_has_membership' ) ) {
echo bl_user_has_membership() ? do_shortcode( 'YOUR_TEMPLATE_SHORTCODE' ) : '';
}
?>
给未授权用户输出指定模板的 Code 元素示例:
<?php
if ( function_exists( 'bl_user_has_membership' ) ) {
echo ! bl_user_has_membership() ? do_shortcode( 'YOUR_TEMPLATE_SHORTCODE' ) : '';
}
?>
也可以用一个 Code 元素同时处理两种情况:
<?php
if ( function_exists( 'bl_user_has_membership' ) ) {
echo bl_user_has_membership() ? do_shortcode( 'YOUR_TEMPLATE_SHORTCODE' ) : do_shortcode( 'YOUR_TEMPLATE_SHORTCODE' );
}
?>
译者注:原文中
do_shortcode()内是本站「查看 300+ 教程」「Pro 访问」两个模板的短代码(正文为截图未抓取到),此处用YOUR_TEMPLATE_SHORTCODE占位,替换成你自己的 Bricks 模板短代码(形如[bricks_template id="XXX"])即可。参考:MemberPress 高级规则。
搜索结果页:按是否有匹配结果渲染不同模板
自定义函数:
// Function to check if there is at least 1 search result.
function bl_has_search_results() {
global $wp_query;
return $wp_query->found_posts !== 0;
}
用法示例:原文只给出函数本体,并说详细教程后续会出(后续已有专门教程,如 “At Least 1 Search Result Condition in Bricks”)。基本用法与上文一致:在搜索结果模板的 Code 元素里,根据 bl_has_search_results() 的返回值输出不同模板的短代码。
文章有特色图片时显示条件区块
见专门教程:Conditional Section when Post has a Featured Image in Bricks。
结论
在 Bricks 内置条件功能出现之前,这套「Section 模板 + Code 元素 + do_shortcode()」的组合就是标准做法:自定义函数判断条件 → Code 元素按条件输出不同模板的短代码。注意两点:①function_exists() 包住自定义函数调用更稳妥;②条件不满足时页面会残留一个空 div,介意的话需要额外处理。如今 Bricks 已内置条件功能,新项目优先用内置条件,本文方案适合老版本或特殊逻辑场景。