WordPress check if post is in category then do this… using custom categories

Ok im not sure if I have worded the title right but basically I have posts in custom categories and I want to get the current post category and do something.

So for example: if current post is in tag_id 15, say ‘your in the category vegetarian’

Read More

this is what im using:

http://www.example.com/wp-admin/edit-tags.php?action=edit&taxonomy=food_category&tag_ID=15&post_type=vegetarian

any help would be great, thank you

Related posts

Leave a Reply

1 comment

  1. WordPress has a built in function for this: in_category( $category, $_post ) (used inside the loop):

    <?php if( in_category( 'vegetarian' ) ): ?>
       You're in the vegetarian category
    <?php endif; ?>
    

    That’s good for a specific use case. If you always want it to spit out your “You’re in the [category] category” for every category, including future ones, you would use get_the_category( $id ) like this example (inside the loop).

    <?php
    $categories = get_the_category( $id );
    if( $categories ){
      // Assumes you just want the first category
      print 'You’re in the ' . $categories[ 0 ]->name . ' category';
    }
    ?>