【问题标题】:PHP conditional statement help on WordpressWordpress 上的 PHP 条件语句帮助
【发布时间】:2011-12-13 08:27:51
【问题描述】:

我正在使用 Wordpress。我有一个名为http://filmblurb.org 的电影评论网站。对于我的博客文章,我正在尝试创建具有不同类别的文章。在“评论”类别下,我有一个“详细信息”框,用作我所有评论的元信息。问题是当我尝试创建具有“功能”或其他类别的帖子时,“详细信息”框仍然存在。基本上,我想尝试创建一个 PHP if 语句,当我只写一篇“评论”帖子时,它只会返回以下代码序列。我正在使用 Wordpress 中的“get_post_meta”标签为我写的每个“评论”帖子填写此“详细信息”框。可以在此处找到示例帖子:http://www.filmblurb.org/reviews/97。有人可以帮我吗?我会很感激。如果我需要更多解释,请告诉我。

<div class="box">
    <div class="boxheader">Details</div>
    <div class="text">
    <h1>Genre</h1>
    <p><?php echo get_post_meta($post->ID, 'genre', true); ?></p>
    <h1>Rated</h1>
    <p><?php echo get_post_meta($post->ID, 'rated', true); ?></p>
    <h1>Release Date</h1>
    <p><?php echo get_post_meta($post->ID, 'releasedate', true); ?></p>
    <h1>Runtime</h1>
    <p><?php echo get_post_meta($post->ID, 'runtime', true); ?></p>
    <h1>Director</h1>
    <p><?php echo get_post_meta($post->ID, 'director', true); ?></p>
    <h1>Cast</h1>
    <p><?php echo get_post_meta($post->ID, 'cast', true); ?></p>
    <h1>Grade</h1>
    <p><?php echo get_post_meta($post->ID, 'grade', true); ?></p>
    </div>
</div>

【问题讨论】:

    标签: php wordpress if-statement echo categories


    【解决方案1】:

    您有多种选择。

    最好的方法可能是为每个帖子类别创建一个特殊的模板。 如果您不知道模板是什么,请在此处了解所有相关信息: http://codex.wordpress.org/Pages#Page_Templates

    其他方法在 CSS 中。

    在正文标签中,您可以输入短代码,该代码将返回有关您当前类别页面的一些信息。

    通过使用特定的类,您可以将 .box 类设置为 display:none。

    我希望这已经足够清楚了。

    【讨论】:

    • 很遗憾地指出,这几乎不值得一读。 page 模板之所以这样命名是有原因的。它们适用于页面,而不是帖子。理论上 .box div 可以被 css 隐藏,但即便如此,它也必须根据类别赋予额外的类或 ID。这又需要 is_category 条件标签。用代码示例证明我错了,但我看不出shortcodes 是如何做到这一点的。实际上,这个答案只值得一票否决,但我今天过得很好。
    【解决方案2】:
     <?php if(is_category('reviews')) : ?>
         <div class="box">
             <div class="boxheader">Details</div>
             <div class="text">
                 <h1>Genre</h1>
                 <p><?php echo get_post_meta($post->ID, 'genre', true); ?></p>
                 <h1>Rated</h1>
                 <p><?php echo get_post_meta($post->ID, 'rated', true); ?></p>
                 <h1>Release Date</h1>
                 <p><?php echo get_post_meta($post->ID, 'releasedate', true); ?></p>
                 <h1>Runtime</h1>
                 <p><?php echo get_post_meta($post->ID, 'runtime', true); ?></p>
                 <h1>Director</h1>
                 <p><?php echo get_post_meta($post->ID, 'director', true); ?></p>
                 <h1>Cast</h1>
                 <p><?php echo get_post_meta($post->ID, 'cast', true); ?></p>
                 <h1>Grade</h1>
                 <p><?php echo get_post_meta($post->ID, 'grade', true); ?></p>
             </div>
         </div>
     <?php endif; ?>
    

    参数可以是类别名称、slug 或 ID。 如需进一步参考,请查看the wordpress codex on the conditional tag "is_category()"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-13
      相关资源
      最近更新 更多