|

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

  • GitGutter for Sublime Makes Diffs Easy

    At Crowd Favorite we use git for everything, which is a nice change from my old workflow of hoping for the best. I’ve learned quite a bit about git and Github since starting and have looked for tools to help me do things better. A really simple, but super helpful, tool is the GitGutter package for…

  • 100 Words 040

    For today’s 100 Words, I’d like to deflect to the Organizers of WordCamp US. It’s sure to be a great event with lots of amazing people, and the first one outside of San Fransisco. If you can come you definitely should. I know I’ll be there! WordCamp US 2015, held December 4–6 in Philadelphia, Pennsylvania,…

  • |

    Using Back Tap on iPhone

    I had the pleasure of participating in the GoWP Niche Agency Owners Happiness Hour recently and we talked all about tools (I didn’t know that going in, but as you can imagine I was pleased). Something I got to demo was how to turn on iPhone Back Tap (a feature in iOS 14 and up)….

  • |

    WordPress and .gitignore

    Updates have been added after the jump! In the coming weeks I’ll likely put a lot of my own personal code (as in, not code people pay me to write) in public repos on Github. A lot of my work is WordPress related so I’ll make a local repo at the root of some WordPress…

  • |

    Excerpt from Building WordPress Themes from Scratch

    Recently my first book, Building WordPress Themes from Scratch, made its way back onto the market. In this book, I teach you how to use WordPress as well as how to leverage the API to create your own custom themes, plugins, and content types. Here is an excerpt from the book. Enjoy!

  • Happy Anniversary, How I Built It!

    One year ago today, I launched Episode 1 of How I Built It, the podcast I started to learn from other business owners. I announced it by telling the story of Il Duomo, which I had seen in person 2 months prior. That’s still a great story that gets at the heart of what I’m trying…