Add new class to css if post_meta_value matches

I am trying to add a new class to posts so that I can style that class differently via css. The current class of the div is box-layout for all post types event_listing. I want that if any post’s meta_key _featured has a meta_value of 1 then it should add a new class before or after the current class. It should be like box-layout featured-class.

Below is the code I am trying to use it in my theme functions.php file but haven’t been successful yet.

add_filter('body_class','add_featured_class');
function add_featured_class ( $classes ) {
    global $post;
    if ( "1" == get_post_meta( get_the_ID(), '_featured', true )  ) {
        $classes[] = 'featured-class';  
    }
    // return the $classes array
    return $classes;
}

Read more here: Add new class to css if post_meta_value matches

Leave a Reply

Your email address will not be published. Required fields are marked *