php if语句中的if语句

时间:2014-07-15 07:00:21

标签: php wordpress

我似乎无法让它发挥作用。我有<?php if (have_posts())...etc.的if语句。然后,在下面我有一个条件语句,确定帖子是否属于某个类别<?php if (is_category())...etc.。这是我无法做到的部分。我做错了什么?

<?php get_header();?>

<section id="content">

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

        <div class="article-wrapper">
            <article id="post-<?php the_ID(); ?>">
                <time datetime="<?php the_time('c'); ?>"><?php the_time('F j, Y'); ?></time>

                <?php if (is_category('news')) { ?>

                <h3><?php the_title(); ?></h3>
                <?php the_excerpt(); ?>
                <p class="read-more"><a href="<?php the_permalink() ?>" rel="bookmark" title="Read more">Read more</a></p>

                <?php } ?>

                <?php if (is_category('podcasts')) {

                $custom = get_post_custom($post->ID);
                $buzzsprout_code = $custom["buzzsprout_code"][0];
                echo do_shortcode($buzzsprout_code);

                echo '<p class="read-emails"><a href="<?php the_permalink() ?>" rel="bookmark" title="View emails and comment on this episode.">View emails and comment on this episode</a></p>';

                } ?>

            </article>
        </div>

    <?php endwhile;endif; ?>

    <div id="pagination">
        <?php my_paginate_links(); ?>
    </div>

</section>

<?php get_sidebar(); ?>
<?php get_footer(); ?>

1 个答案:

答案 0 :(得分:2)

来自wordpress docs:

  

is_category()测试是否显示类别存档,但是您显示的是一个帖子,因此这将始终返回false

     

in_category()测试一个给定的帖子是否在该特定类别中有一个类别,并且必须在The Loop中使用,但是在进入循环之前,你试图弄清楚该类别是什么。

简短的尝试in_category()代替。

相关问题