if …。 else用作条件语句或条件表达式,如果特定条件返回true,则使您可以执行代码;如果相同条件为false,则可以执行其他代码。
范例:
if ( is_front_page() ) {
echo 'This is your front page';
} else {
echo 'This text prints on all pages excluding your front page';
}
您可以通过将上述代码添加到您的子主题函数文件中来测试上述代码,但是您可以’首先需要将其包装在 动作钩 像这样 :
add_action( 'genesis_after_header', 'if_else_code_test' );
function if_else_code_test() {
if ( is_front_page() ) {
echo 'This is your front page';
} else {
echo 'This text prints on all pages excluding your front page';
}
}
您还可以将条件标签与 三元运算符 达到相同的结果。
if…elseif….else #
if …. elseif …。否则,如果特定条件返回true,则使您可以执行代码;如果特定条件返回false,则使您可以执行代码。 看视频.
范例:
if ( is_front_page() ) {
echo 'This prints on your front page';
} elseif ( is_singular('post') ) {
echo 'This text prints on all single posts excluding your front page';
} else {
echo 'Print this on all pages if all other conditions return false.
}
要测试,修改和使用此代码,您可以将此代码粘贴到子主题函数文件的末尾, 任何钩子 像这样。
add_action( 'genesis_after_header', 'if_elseif_else_code_test' );
function if_elseif_else_code_test() {
if ( is_front_page() ) {
echo 'This prints on your front page';
} elseif ( is_singular('post') ) {
echo 'This text prints on all single posts excluding your front page';
} else {
echo 'Print this on all pages if all other conditions return false';
}
}
上面的代码在首页上打印唯一文本,在所有单个帖子上打印唯一文本,在所有其他页面/帖子类型上打印唯一文本。
看视频 #
以下视频使用上面的代码段( 加上样式& different hook )演示如何使用 如果其他 任何Genesis子主题中带有条件标签的条件语句。
发表评论
你一定是 登录 发表评论。