Archived topic
Setting primary navigation menu item as current
3 replies · Started by Roger on February 19, 2019
Hi, I have set a custom blog posts page titled 'news' and want the 'news' navigation item to remain selected as current for all individual blog posts, category pages and tag pages.
I adapted the code from this topic and added it as an inside_navigation hook but it doesn't appear to be working.
<?php
function be_menu_item_classes( $classes, $item, $args ) {
if( 'header' !== $args->theme_location )
return $classes;
if( ( is_singular( 'post' ) || is_category() || is_tag() ) && 'news' == $item->title )
$classes[] = 'current-menu-item';
return array_unique( $classes );
}
add_filter( 'nav_menu_css_class', 'be_menu_item_classes', 10, 3 );
?>
What am I doing wrong?
Hi there,
You need to add it using one of these methods: https://docs.generatepress.com/article/adding-php/
Also, this line is assuming the menu has a header theme location, which GP doesn't have:
if( 'header' !== $args->theme_location )
You'll want to replace header with primary.
Perfect, thanks Tom :)
You're welcome :)