<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?

3 Answers 3


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="', '<a class="' . $class_to_add . '" href="', $thelist);

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.

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 ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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