Support » Fixing WordPress » if in_category code question

  • Resolved csleh

    (@csleh)


    Can anything be added to this code

    <?php if (in_category('3')) : ?>
    <p class="date">POSTED <?php the_time('F d, Y') ?></p>
    <?php endif; ?>

    that will say “if in category 3 EXCEPT for post 50, show this”

    I tried
    <?php if (in_category('3&exclude=50')) : ?> and (in_category('3&exclude=p50'))
    but that deletes the code on all the posts

Viewing 5 replies - 1 through 5 (of 5 total)
  • The only parameter you can use is the category ID:

    http://codex.wordpress.org/Template_Tags/in_category

    Right, that’s what I understood the code to be.

    Here’s the goal:
    category 3 is news. Everything in news should look the same, generally. There is one post (50) that is info for media. The site uses breadcrumbs, so you can see the path on the page. If you are looking at a press release (99% of news category items), you should see the date posted. But if you are looking at the media info page, the posting date doesn’t make sense.

    There could be a second category that only has the media info, but that would be confusing — not only do I have to pull in an additional category whenever referencing news, but viewers would see a different path in the breadcrumbs.

    My thought was that if I could just exclude one post from the in_category call all these problems would be fixed. I am open to any suggestions?

    I’m sure there is a cleaner way, but adding another if-else inside the first one (for category) to test for a single post with ID of 50 (the media sheet) did the trick.

    <?php if (in_category('3')) : ?>
    <?php if( is_single(50) ) : ?>
    <?php else : ?>
    <p class="newsdatepage">TROY, MICHIGAN &mdash; <?php the_time('F d, Y') ?>
    
    <?php endif; ?>
    <?php endif; ?>

    Cleaner way:

    <?php if (in_category('3') && !is_single(50)) : ?>
    <p class="newsdatepage">TROY, MICHIGAN &mdash; <?php the_time('F d, Y') ?>
    <?php endif; ?>

    OK, so the "&& is saying “if this is also true” and the ! is saying “leave this one out”. Is that right?

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘if in_category code question’ is closed to new replies.