|

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

  • Caring Enough to Give Back to Your Community

    People who know me probably know a few tenets of my character: I can get very passionate about things, I like doing stuff (to the point where I say yes too much), and I give back where I can. Over the weekend, I hosted an event called the NEPA Hackathon and it was a lot of fun….

  • |

    Why Not Wanting to Pay for Software is Holding Your Business Back

    Earlier this month, Elementor announced that they are increasing their prices. Most notably, 1000 sites (called the Agency Plan) is moving from $199/year to $999/year. This is only for new customers but, as is common when software pricing changes, people are outraged. Not only is this sort of outrage getting old, but I would argue that if you’re not willing to pay what software is worth, you will never grow as a business. And not for the reason you think.

  • |

    Setting All Videos To Show Controls In LearnDash

    One of the biggest pieces of feedback I’ve gotten on my course videos in recent weeks was that the controls on my videos were hidden. To be honest, I didn’t realize students couldn’t scrub through videos, or rewind – I assumed everything was working! After looking through the LearnDash settings and not finding a global…

  • Word Pres 1.5

    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!I just upgraded! YAY. You will probably be looking at this layout for a couple of days which i decide if…

  • The way to learn WordPress is not by contributing

    has happened in WordPress this year, and what we can expect moving forward. As always, he extolled the virtues of open source and the importance of contributing. He spent several minutes on Five for the Future, a program encouraging companies who make money with WordPress to give 5% of their time to the open source project. And while I strongly recommend contributing, I don’t think it’s the path someone should take when they first learn.