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

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.

share|improve this question
up vote 1 down vote accepted

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;
share|improve this answer
don't need it any more but will give u the benefit of the doubt. :) – andrewk Dec 25 '10 at 22:16

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.