|

Quick Tip: Custom Category Styles in WordPress

Note: This article was published while I was in my early 20s. I was much younger and dumber. Please don't hold it against me. One of the perils of having a 20+ year old website!

Over the weekend I revamped this blog’s current theme a little bit, including adding custom styles for my Link Round-up lists. I achieved this by adding a simple piece of code within my loop to make each category a CSS class. Here’s how I did it.

Note: You need to be at least a little bit familiar with the WordPress API, and coding themes.

Go into the theme page where your posts are printed (in most themes, this will be at least 2 places: index.php and single.php) and find The Loop. Here you will either find the div that is wrapping each post or write a div to wrap each post.

For the class, call this function: category_classes() (don’t worry, I’ll provide the code in the next steps). You should have something that looks like this:

<?php while ( have_posts() ) : the_post(); ?>
     <div class="<?=category_classes();?>
          /**You Loop Stuff**/
     </div>
<?php endwhile; ?>

Next, we are going to open up our functions.php and add this function:

function category_classes(){
   global $post;
   $cats= "";
   foreach((get_the_category($post->ID)) as $category) {
      $cats.= $category->category_nicename . ' ';
   }
   return trim($cats);
}

This function grabs the categories of the current post, and adds each of their names (or in this case, the slug to make is a CSS friendly class name) to a string. It returns that string, trimming any extra white space from the ends.

Since we printed it in the first step, now all we have to do is add the CSS. For the WordPress nicenames (or slugs), generally the slug will be all lowercase, where spaces are replaced by dashes (-). So if I have a category called “Quick Tips,” I would define the CSS class .quick-tips.

And there you have it! You can now create different styles for any (or all) of your categories! Just keep in mind, this adds all of the category slugs as css classes, so you may have some conflicts (ie if you have a post in Category A and Category B, you will get class="category-a category-b").

Similar Posts

  • |

    Quick Tip: Add a Widget to WordPress

    Note: This article was published while I was in my early 20s. I was much younger and dumber. Please don’t hold it against me. One of the perils of having a 20+ year old website!A couple of weeks ago I was working on a WordPress site and thought it might be nice to add a…

  • Writing a User log-in

    Note: This article was published while I was in my early 20s. I was much younger and dumber. Please don’t hold it against me. One of the perils of having a 20+ year old website!Here is the user login code I have been promising. It’s an image so it looks nice. You can download it…

  • |

    Why Pro Bono Work Hurts Everyone

    It’s something that has happened to everyone who’s a freelancer, or perhaps just everyone who runs a business. People will ask for things for free; I want to make it super clear now that I’m not saying never do pro bono work. I’ve done it. It just needs to be the right circumstances; asking a…

  • |

    QT: Increase the WordPress RSS Widget Refresh Rate

    Note: This article was published while I was in my early 20s. I was much younger and dumber. Please don’t hold it against me. One of the perils of having a 20+ year old website!While working on the redesign to this blog, I wanted to add the “Interesting Stuff” column you see just to the…

  • Building Parsec: Speaking at WordCamp Lancaster

    This Saturday (February 28th) I will be speaking at the 2nd Annual WordCamp Lancaster. I had an absolute blast last year, so I’m really looking forward to this one. It’s being held at Intermediate Unit 13 and there are still some tickets left, so if you can come I strongly recommend it!

  • | | |

    Winn Cards and Comics

    Note: This article was published while I was in my early 20s. I was much younger and dumber. Please don’t hold it against me. One of the perils of having a 20+ year old website! The Winn Site is a project I have been working on for a couple of months now. The guys wanted…