0

I can't seem to get this to work. I have an if statement opening with <?php if (have_posts())...etc.. Then, below that I have a conditional statement determining whether or not the post is in a certain category <?php if (is_category())...etc.. This is the part I can't get right. What am I doing wrong?

<?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(); ?>
4
  • Looks fine at first glance. What's the actual problem? Commented Jul 15, 2014 at 7:03
  • @Phil The posts don't show up. Do I have to close the if tags for the two is_category() conditionals? Commented Jul 15, 2014 at 7:09
  • Check the category wording, make sure it is the same with the one in the is_category function's argument. Commented Jul 15, 2014 at 7:12
  • I think you need use this one instead in_category() Commented Jul 15, 2014 at 7:12

1 Answer 1

2

From wordpress docs:

is_category() tests to see if you are displaying a category archive, but you are displaying a single post, so this will always return false

in_category() tests to see if a given post has a category in that specific category and must be used in The Loop, but you are trying to figure out what the category is, before you get to the loop.

The short try in_category() instead.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.