WordPress.org

Ready to get started?Download WordPress

Forums

Find Category ID of post, see if it equals 20, then do something (4 posts)

  1. dave_merwin
    Member
    Posted 8 years ago #

    I am trying to find the categories of a post, and see if one of them is a specific id. If it is... do something. ELSE, do something else.

    My logic is that I need to build a list of the IDs for a post. Then I need to see if one of those IDs is 20. Then do the if/else.

    What is wrong with my code below? This is in the loop by the way.

    <?php
    foreach ((get_the_category()) as $cat) {
    $my_cat .= $cat->category_id . ',';
    }
    echo $my_cat; //FOR DEBUGGING

    if ($my_cat == '20') { ?>

    <p>DO SOMETHING</p>

    <?php } else { ?>

    <p>DO SOMETHING ELSE</p>

    <?php } ?>

  2. Try:

    <?php if ( in_category(20) ) { ?>
    <p>DO SOMETHING</p>
    <?php } else { ?>
    <p>DO SOMETHING ELSE</p>
    <?php } ?>

  3. dave_merwin
    Member
    Posted 8 years ago #

    Perfect... worked like a dream. Thanks.

  4. HapaxLegomen
    Member
    Posted 8 years ago #

    Sniff !! :-(
    Me I need more complex conditions and I crash on the same problem ! But the solution shown can be applicated to my case ! Heeeeeeeelp... Sniff !

Topic Closed

This topic has been closed to new replies.

About this Topic