Skip to content

Instantly share code, notes, and snippets.

@fjaguero
Created February 28, 2013 11:01
Show Gist options
  • Save fjaguero/5055950 to your computer and use it in GitHub Desktop.
Save fjaguero/5055950 to your computer and use it in GitHub Desktop.
Wordpress: wp_list_categories -> Generate the current-cat class when viewing single posts.
/* @Workether ------------------------------------------------------------
Generate the current-cat class when viewing single posts.
--------------------------------------------------------------------------*/
class singlePostCurrentCat {
function wp_list_categories ($text) {
global $post;
$categories = wp_get_post_categories($post->ID);
foreach ($categories as $category_id) {
$category = get_category($category_id);
$text = preg_replace(
"/class=\"(.*)\"><a ([^<>]*)>$category->name<\/a>/",
' class="$1 current-cat"><a $2>' . $category->name . '</a>',
$text);
}
return $text;
}
}
add_filter('wp_list_categories', array('singlePostCurrentCat','wp_list_categories'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment