Google Groups

Re: [wordpress-austin] setting post background color - only via CSS not on the fly?

Shelly K. Apr 28, 2012 6:05 PM
Posted in group: WordPress Austin
Wow -

Thanks for taking the time to spell it out, Brandtley. I appreciate the correct answer.

It's a little over my head, so I'll print it out and break it down.

Thanks again,
Shelly K.

On 4/28/2012 3:16 PM, Brandtley McMinn wrote:
Hey Shelly,

You would be better off defining a function in your themes' functions.php file that adds the post category to the post_class filter hook on the post container.

For example:


  // Practically verbatim from the WordPress codex


  function MYTHEME_post_class_cats($classes) {
    global $post;
    foreach((get_the_category($post->ID)) as $category) {
      $classes[] = $category->category_nicename;
      return $classes;
  } // MYTHEME_post_class_cats($classes)


All this does is it uses the post ID to determine its category, and then adds the category slug name to the array of html class attributes that are outputted when the page loads.

So you effectively get this as your markup:
<div id="post-example-342" class="post single hentry POST_CATEGORY">

Where POST_CATEGORY is actually the lowercase hyphenated slug which you can then target in your theme CSS as you were attempting to do before.

You can also add any classes names/strings you want by using $classes[] = 'your-string-here'.$orStringVariable; which appends your new string to the array.

Hope this helps,

Brandtley McMinn - Owner/Creative Director
c. 512.406.1666

Saturday, April 28, 2012 2:38 PM
Hello WP Experts out there, WP beginner here.

Would you please confirm the following regarding setting the background color of a post..

After considerable searching, it seems i can't set the background color of an individual post on the fly - is this so? Not through the HTML editor (because bgcolor has been deprecated), and there is not an icon as part of the wysiwyg editor.

it seems the only way to really do it is to edit the css file to include something like:

/* Set "sample" category posts to have yellow background */
.entry-category-sample { background-color: #FBEC5D; }

So, the author or category or something else of the post must be defined... again, can't just set the post color on the fly, eh?

Please advise.

Thank you,
Shelly K.

You received this message because you are subscribed to the Google Groups "WordPress Austin" group.
To post to this group, send email to
To unsubscribe from this group, send email to
For more options, visit this group at
Our meeting information is available at