22 | Highlight the post’s categories in the category list


adding a function call to a template file to get the category highlight effect is not always achievable, for instance if the list is shown in the sidebar.
if the standard category widget is used to display the list, marking the current categories – i.e. the categories in the single post – is better done using a filter hook.

// filter to add the .current-cat class to categories list in single post
function style_current_cat_single_post($output) {
if( is_single() ) :
global $post;
$categories = wp_get_post_categories($post->ID);

foreach($categories as $value) {
if(preg_match('#item-' . $value . '">#', $output)) {
$output = str_replace('item-' . $value . '">', 'item-' . $value . ' current-cat">', $output);
return $output;

the programming is basicly the same as in the function from page 1.

This entry was posted in Tips & Tricks, WordPress and tagged , , , . Bookmark the permalink.

One Response to Highlight the post’s categories in the category list

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>


This site uses Akismet to reduce spam. Learn how your comment data is processed.