Forums

wp_list_categories( ) w/ img before-after & Show Active Category functionality (1 post)

  1. crisjb
    Member
    Posted 2 years ago #

    That title is a mouthful, but that's what I'm attempting to do. I discovered a neat trick to add elements via <span>, <div>, or otherwise in the code for each category listing here. Now the new challenge is to make this category

  2. listings show an active category for the current single page's category. Here's the code for the Show Active Category plugin:
    function show_active_category($text) {
    
    	global $post;
    
    	if( is_single() || is_category() ) {
    
    		$categories = wp_get_post_categories($post->ID);
    
    		foreach( $categories as $catid ) {
    			$cat = get_category($catid);
    			if(preg_match('#>' . $cat->name . '</a>#', $text)) {
    				$text = str_replace('>' . $cat->name . '</a>', ' class="active-cat">' . $cat->name . '</a>', $text);
    			}
    		}
    
    	}
    
    	return $text;
    
    }
    
    add_filter('wp_list_categories', 'show_active_category');

    This plugin works great alone, but I'm trying to combine the two. I tried canging variable names so there's no conflict. I even changed the add_filter('wp_list_categories... to add_filter('get_categories... but that didn't work either.

    Any help would be much appreciated.

    thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags