• 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 } ?>

Viewing 3 replies - 1 through 3 (of 3 total)
  • Scott Reilly

    (@coffee2code)

    WordPress & Plugin Developer

    Try:

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

    Thread Starter dave_merwin

    (@dave_merwin)

    Perfect… worked like a dream. Thanks.

    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 !

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Find Category ID of post, see if it equals 20, then do something’ is closed to new replies.