1

I'm trying to have the post background colors change depending on a meta value for the specific post type rather than using the post status. Looked everywhere and can't find a solution. (perhaps there isn't one?)

Specifying post colors based on post-status is simple enough

add_action('admin_footer','posts_status_color');
function posts_status_color(){
?>
<style>
.status-draft{background: #FFFF98 !important;}
.status-pending{background: #FFFF98 !important;}
.post-*id here*{background: #FFFF98 !important;}
.status-publish{/* no background keep wp alternating colors */}
</style>
<?php
}

How do I specify the colors based on a custom meta key/value from the post?

1

1 Answer 1

3

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.

3
  • Excellent im happy it worked!
    – Vigs
    Jul 17, 2013 at 23:03
  • Thought I'd mention a typo in the add_filter section. add_filter('post_class', 'post_classes'); should be add_filter('post_class', 'posts_classes'); Jul 17, 2013 at 23:06
  • good catch. will update
    – Vigs
    Jul 18, 2013 at 0:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.