Created
February 28, 2013 11:01
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* @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