wordpress – identfy category id from post then if statement

Sorry im a beginner. im trying to write an if statement based on the category id of the post on a wordpress blog.

here is my code so you get an idea

Read More
<div class="col-md-8">
    <?php if (have_posts()) : ?>    
    <?php while (have_posts()) : the_post(); ?>

<div class="panel <?php

if (in_category(1) ){
    echo "panel-green";
    }
if (in_category(2) ) {
    echo "panel-primary";
    }
if (in_category(3) ) {
    echo "panel-yellow";
    }
if (in_category(4) ) {
    echo "panel-success";
    }
if (in_category(5)){
    echo "panel-info";
    }
if (in_category(6) ){
    echo "panel-red";
    }
?>">

        <div class="panel-heading">

so the idea is that you have different panel colours relating to the category, but i cant get it right, its picking up the category’s but in the wrong order

Any help would be much appreciated

Related posts

Leave a Reply

1 comment

  1. This is simple … suppose you have post_id in a variable $post_id then you will need to run this query to get category_id(term_id) of that particular post

    $myval = $wpdb->get_results("SELECT `term_taxonomy_id` FROM 
    `wp_term_relationships` WHERE `object_id` = '$post_id'", ARRAY_A);
    

    then you can directly get your term_id using $myval[0]['term_taxonomy_id']