Custom colors for post rows based on post meta value

If your theme uses wp post class

function post_classes($classes) {
    global $post;
        $customMetaVariable = get_post_meta( $post->ID, 'customMetaName', true );
    if($customMetaVariable == 'desiredCustomMetaValue'){
        $classes[] = 'cssClassName';
        return $classes;
    }
}
add_filter('post_class', 'post_classes');

then in your style.css you could use:

.cssClassName{
background-color: red;
}

hence applying that class to all of the posts that contain your desired meta value.

if your theme doesn’t use wp post class you have to edit the theme to include

<?php post_class(); ?>

ex:

<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
//post stuff hurr
</div>

all explained here.

Leave a Comment