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
<h1><?php the_category(' &bull; '); ?> » <a href="<?php the_permalink(); ?>" class="my-title-class"><?php the_title(); ?></a></h1>

How to add class="my-category-class" to the links, produces by the_category function?

share|improve this question
up vote 2 down vote accepted

You can use the_category filter to hook a callback function like this:


function add_class_to_category( $thelist, $separator, $parents){
    $class_to_add = 'my-category-class';
    return str_replace('<a href="', 'class="'. $class_to_add. '" <a href="', $thelist);
share|improve this answer

Pick your choice:

  • style categories by already existing CSS definition: .post-categories a {YOUR_STYLE_HERE};
  • use jQuery to add your class: $("link-categories a").addClass("my-category-class");;
  • write your own function to echo categories with your own classes/attributes.
share|improve this answer

As a beginner I would try creating my function, that I would call instead of the_category();

function my_category_func($sep, $classname)
        $output = null;
        $output = '<div class="$classname">'.the_category( $sep ).'</div>';
        return $output;

I believe this could be done more professionally, including all the variables, but I'd go this way for starters ;)

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.