WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] if in_category code question (6 posts)

  1. csleh
    Member
    Posted 5 years ago #

    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

  2. iridiax
    Member
    Posted 5 years ago #

    The only parameter you can use is the category ID:

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

  3. csleh
    Member
    Posted 5 years ago #

    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?

  4. csleh
    Member
    Posted 5 years ago #

    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; ?>
  5. VelvetBlues.com
    Member
    Posted 5 years ago #

    Cleaner way:

    <?php if (in_category('3') && !is_single(50)) : ?>
    <p class="newsdatepage">TROY, MICHIGAN &mdash; <?php the_time('F d, Y') ?>
    <?php endif; ?>
  6. csleh
    Member
    Posted 5 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic