• Nuuk

    (@nuuk)


    Hi,

    I’ve searched the forum and found relevant entries, but still haven’t been able to make this work.

    I want to highlight the current category on my site: https://nilaska.dk/blog/categories/cph-vision/

    So that if you click “CPH Vision” and you’re on: https://nilaska.dk/blog/categories/cph-vision/, “CPH Vision” is black, like in the menu when your on Information for example.

    I’ve done this with the menu, but manually in the code. With the categories I’d like to try and make it automatic.

    Can anybody help?

    Thanks in advance,
    Nilas

Viewing 2 replies - 1 through 2 (of 2 total)
  • Chip Bennett

    (@chipbennett)

    Here’s what I use in my Theme:

    /************************************************************************
    * Add a "current-cat" CSS class declaration
    *************************************************************************/
    
    /* Credit: StudioGrasshopper
    *	https://www.studiograsshopper.ch/code-snippets/dynamic-category-menu-highlighting-for-single-posts/
    */
    function oenology_show_current_cat_on_single($output) { 
    
    	global $post;
    
    	if( is_single() ) {
    
    		$categories = wp_get_post_categories($post->ID);
    
    		foreach( $categories as $catid ) {
    			$cat = get_category($catid);
    			// Find cat-item-ID in the string
    			if(preg_match('#cat-item-' . $cat->cat_ID . '#', $output)) {
    				$output = str_replace('cat-item-'.$cat->cat_ID, 'cat-item-'.$cat->cat_ID . ' current-cat', $output);
    			}
    		}
    
    	}
    	return $output;
    }
    add_filter('wp_list_categories', 'oenology_show_current_cat_on_single');

    Thread Starter Nuuk

    (@nuuk)

    It works perfect, thank you very much!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Hightlight current category’ is closed to new replies.