【问题标题】:WordPress Conditional Content by Multiple Post Types多种帖子类型的 WordPress 条件内容
【发布时间】:2013-03-20 08:49:46
【问题描述】:

致力于一个高度复杂的 WordPress 主题,其中包含许多不同的 custom post types。我的印象是,以下代码将检查当前帖子是否属于“哲学家”类型,然后仅当帖子属于该类型时才运行其后面的 PHP 条件:

<?php if (get_post_type() == 'philosopher') { ?>

不幸的是,我的期望是让相同的项目有条件地用于“哲学家”、“文本”和“原始”,我的代码应该如下所示:

<?php if (get_post_type() == 'philosopher','text','original') { ?>

可惜,我错了。我确实得到了以下代码,但我想知道是否有更清洁的方法来做到这一点(或者我是否遗漏了一些东西)

        <?php if ( (get_post_type() == 'philosimply') || (get_post_type() == 'text') || (get_post_type() == 'original'))  { ?>

【问题讨论】:

    标签: php wordpress syntax


    【解决方案1】:
    if ( in_array(get_post_type(), array('philosopher','text','original')) ){ ... }
    

    http://www.php.net/manual/en/function.in-array.php

    这就是你要找的吗?

    HTH

    【讨论】:

    • 实际上,我收回了-您在此处发布的代码不起作用:(
    • 其实不是固定的 Andreas - 你需要 3 个右大括号,而你只有 2 个。应该是:if (in_array( get_post_type(), array('philosopher','text','original'))){ ... }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多