WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm customizing a WordPress installation and was wondering what the best way to do this is. I want to customize each post completely depending on the category. Each post will be under only one category.

share|improve this question

for the css route, you can use the post_class() function to give you category dependant css classes.

if you go beyond formatting, i.e. output different elements depending on the category, you could use the conditional tag in_category() in an 'if-elseif-else' structure.

the category archives can be done with category templates.

share|improve this answer

There're 2 functions that help you handle the classes in WP: body_class() and post_class(). When you use body_class(), you can style the post that in a certain category like this (in your CSS file):

/* normal rule for all posts */
.post-title {font-size: 22px; color: #0f0}

/* for posts in Talks category only */
.category-talks .post-title {font-size: 28px; color: #fff}
share|improve this answer

If you want total control, then a css solution is not enough. a more powerful solution is to create a sort of template hierarchy for single posts based on the category slug. Here is the general idea.

In single.php find out the categories connected to the post using get_the_category() - if as you say there will only ever be one category assigned, then just use the first value in the returned array. then once you have the cat object, get it's slug. from there see if that filename exists in the theme folder that matches 'single-' plus catslug and load it if it does. Otherwise continue using the regular single post loop. then you create post templates for specific categories and name them based on the category slug. ie. single-mycatslug.php.

here is some code:

$my_cats = get_the_category();

if ( is_array( $my_cats ) ) {
    $my_cat_slug = $my_cats[0]->slug;

$my_template = 'single-' . $my_cat_slug . '.php';

if ( file_exists( TEMPLATEPATH  . $my_template ) ) {
    get_template_part( $my_template );
} else {
    // run normal loop for single post

I run this on a production site and it works great.

share|improve this answer

Add a custom class in your functions file to style all posts in a category the same as well as the category archive page.

add_filter( 'body_class', 'wpsites_add_body_class' );
function wpsites_add_body_class( $classes ) {
if ( in_category('8')) {
   $classes[] = 'your-custom-class';
   return $classes;

And some sample CSS:

.your-custom-class {
    background-color: red;
    color: #fff;
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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