• crisjb

    (@crisjb)


    Hi,
    I’m trying to highlight the active category of a post on a single page and on the category page. At the same token I need to add div elements to the markup within the anchor tags of the category links because the current nav design requires rounded corners. So in essence I’m injecting div elements within the li tags and and also highlighting the active category through the “active-cat” class. Here’s the code that I recycled from a plugin. It works to highlight the current category but it doesn’t inject the div elements to the additional category links. Im running this code in the functions.php file. Any help is appreciated. Thanks!

    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"><div id="edgeL"></div>' . $cat->name . '<div id="edgeR"></div></a>', $text);
    			else:
    				$text = str_replace('>' . $cat->name . '</a>', ' ><div id="edgeL"></div>' . $cat->name . '<div id="edgeR"></div></a>', $text);			
    
    			endif;
    
    		}
    
    	}
    
    	return $text;
    
    }
    
    add_filter('wp_list_categories', 'show_active_category');

  • The topic ‘Highlight active category in Single Page’ is closed to new replies.