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.

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.

like

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

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
add comment

1 Answer

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
add comment

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.