Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.
<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

3 Answers 3

up vote 2 down vote accepted

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

add_filter('the_category','add_class_to_category',10,3);

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

 
discard

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.