• Hello. First of all I did search before asking this question. With that being said, I still cannot figure this out.

    I have a couple of different menu items that link to different category archives. Whenever I click on the menu item, it is highlighted. However, when I click one of the articles to read (or go directly to the article), the menu item is not highlighted. How can I achieve this? I want the menu item to remain highlighted even when I am reading a single post.

    Like I said, I did search and found some code. However, none of the posts that I read said where they inserted the code. Thanks for your help.

    Rusty

Viewing 3 replies - 1 through 3 (of 3 total)
  • it always helps to quote the resources you have already tried.
    a link to the site with the issue can also be of use.

    is your menu using wp_list_categories() ?

    then you might be able to use a filter:

    add_filter('wp_list_categories','highlight_single_posts_categories');
    function highlight_single_posts_categories($output) {
    if(is_single()) :
    $categories = wp_get_post_categories(get_the_ID());
    foreach ($categories as $catid) {
    $cat = get_category($catid);
    $cats[] = $cat->cat_ID;
     }
    if($cats) { foreach($cats as $value) {
    	if(preg_match('#item-' . $value . '">#', $output)) {
    	$output = str_replace('item-' . $value . '">', 'item-' . $value . ' current-cat">', $output);
    	}
    	}
    }
    endif;
    return $output;
    }

    needs to be added to functions.php of your theme.

    Thread Starter rpainter

    (@rpainter)

    Sorry about that. Here is a link to my site: Our Plastic Life

    The menu is the top navigation menu. I have no idea if it is using wp_list_categories(). It is using whatever WP uses in Appearance>Menus. I guess it’s not using that because the code you provided did not work.

    Thanks for your help.

    Rusty

    Hi Rusty,

    I’m also having the same issue with the menu system (3.01) it seems that you can target page ancestors from posts when your menu system is set up to use categories, but not if your firing links to posts from a custom loop on a custom template.

    This page was rasonably helpful https://www.designisphilosophy.com/tutorials/highlight-current-page-or-category/ but didn’t crack it, to make things more complicated I’ve been using cufon fonts, but I’ve got that bit nailed now at least.

    I just checked your site and looks like you didn’t get any further (good job btw) is this still the case.

    Thanks,

    Anthony

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Highlight Menu Item When Viewing Single Post’ is closed to new replies.