• 跳至主要导航
  • 跳到主要内容
  • 跳到主要侧边栏

WP站点

2665

原始创世纪教程和5000多个保证代码

片段

  • 支持
  • 通讯
  • 影片
  • 登录

高级会员? -请求自定义代码

如果别的

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子主题中带有条件标签的条件语句。

相关教程

  • 什么是三元运算符
  • 有条件的创世纪发布信息

PHP函数

读者互动

发表评论 取消回复

你一定是 登录 发表评论。

主侧边栏

PHP代码

template_include

get_body_class

如果别的

数组

class_exists

前言

冲刺

add_action

打印

变量

广告 · WPEngine · 创世记 · 登录

  • 高级会员的工作方式
  • 报名
  • 支持
  • 订阅详细信息/发票
  • 标记教程
  • 访问下载问题