Category menu issues

Hey Guys,

when using Sage, I am having some navigation issues.

I am trying to add a class to my category links in the navigation, that display when a page in that category is viewed. Doesnt seem to add a class to the nav link though.

i think it may be this snippet in the nav.php

function roots_nav_menu_css_class($classes, $item) {
$slug = sanitize_title($item->title);
$classes = preg_replace('/(current(-menu-|[-_]page[-_])(item|parent|ancestor))/', 'active', $classes);
$classes = preg_replace('/^((menu|page)[-_\w+]+)+/', '', $classes);

$classes[] = 'menu-' . $slug;

$classes = array_unique($classes);

return array_filter($classes, 'is_element_empty');
}
add_filter('nav_menu_css_class', 'roots_nav_menu_css_class', 10, 2);
add_filter('nav_menu_item_id', '__return_null');

any help would be great thanks

Do you mean that you’re trying to add an active class to a category link in a menu when you’re viewing a single post that matches the category?

If so, how are you trying to do it / where is the example code? The snippet you provided appears to be unmodified from a pre-Sage version of the starter theme.

I think you right, code is from a while ago, and it looks like you’ve had a major overhaul.

Yes I do, however they are not posts, they are pages.

I’ve tried a few things copied form the wordpress.org archieves (but nothing that I’ve saved.). With the snippet above adding category & page to item|parent|ancestor

didnt seem to have any effect - then I realisd I was shotgun debugging and thought id ask

thanks for the fast reply btw

1 Like