i know if you are on a category page..wp automatically ads current-cat class to the child of the parent category you are on.


`<li class="cat-item cat-item-701 current-cat"><a href="http://goog.com/cat/subcat">Free Stuff</a>

how can i do this for a singlepost page.

I have the_category(); in the single.php file and it shows both parent and child category. I want to add a current-cat class to the child cat when i am on the single post page.

thanks in advance hopefully i did not confuse you.

1 Answer 1


the_category() uses get_the_category_list() to do its work, but this function gives you no way to specify classes. However, you can filter the output with the the_category hook. Since you know the format of the current category links, you can so a search and replace on them.

It would look something like this (untested):

add_filter('the_category', 'highlight_current_cats', 10, 3);
function highlight_current_cats($thelist, $separator, $parents)
    // The current cat links will look like <a href="[category link]" [other stuff]
    // We want them it look like <a href="[category link]" class="current-cat" [other stuff]
    $current_cats = get_the_category();
    if ($current_cats) {
        foreach ($current_cats as $cat) {
            $cat_link = get_category_link($cat->term_id);
            $thelist = str_replace('<a href="' . $cat_link . '"', '<a href="' . $cat_link . '" class="current-cat"', $thelist);
    return $thelist;
  • don't need it any more but will give u the benefit of the doubt. :)
    – andrewk
    Commented Dec 25, 2010 at 22:16

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.