Archived topic
How to set menu choice as current in certain pages
32 replies · Started by Harris on October 2, 2017
Hello,
I have made a custom blog page and hanged in the menu (News). When I am in that page the "News" menu choice shows as active (current).
When I click on one of the blog posts in this page, I go to the single blog posts page and the "News" menu is not active any more (obviously).
Is there some way I can set the "News" menu choice as current (active) both when I am in my custom blog page (list) and also when I am in any of my single posts pages?
Thanks!
Hi there,
I believe it's do-able but would require some pretty complicated coding.
There is a similar request here: https://generatepress.com/forums/topic/background-colours-of-menus/ and she ended up finding someone to code it for her.
Thanks!
Isn't that supposed to do something similar?
<?php
/*
* Customize Menu Item Classes
* @author Bill Erickson
* @link http://www.billerickson.net/customize-which-menu-item-is-marked-active/
*
* @param array $classes, current menu classes
* @param object $item, current menu item
* @param object $args, menu arguments
* @return array $classes
*/
function be_menu_item_classes( $classes, $item, $args ) {
if( 'header' !== $args->theme_location )
return $classes;
if( ( is_singular( 'post' ) || is_category() || is_tag() ) && 'Blog' == $item->title )
$classes[] = 'current-menu-item';
if( ( is_singular( 'code' ) || is_tax( 'code-tag' ) ) && 'Code' == $item->title )
$classes[] = 'current-menu-item';
if( is_singular( 'projects' ) && 'Case Studies' == $item->title )
$classes[] = 'current-menu-item';
return array_unique( $classes );
}
add_filter( 'nav_menu_css_class', 'be_menu_item_classes', 10, 3 );
That code is perfect :)
Any chance ot get some help to adopt it in GP?
Did you try adding it or not yet?
If not try one of the methods here: https://docs.generatepress.com/article/adding-php/
Hello,
I am trying to find what the needed parameters are in order to make it work.
Is "News" a category name, or just all of your blog posts?
The category name is "news" and I want to "asign" it in the menu item named "NEA"
In that case, you could do this:
function be_menu_item_classes( $classes, $item, $args ) {
if ( 'primary' !== $args->theme_location ) {
return $classes;
}
if ( ( is_singular( 'post' ) && in_category( 'news' ) ) && 'ΝΕΑ' == $item->title ) {
$classes[] = 'current-menu-item';
}
return array_unique( $classes );
}
add_filter( 'nav_menu_css_class', 'be_menu_item_classes', 10, 3 );
Thanks a lot Tom!
For some reason I thought I had to change more parameters than the posts category and the menu item name.
No problem! :)
Hello!
Can this be modified to cover single product pages (woocommerce) instead of single posts pages?
Thanks
Hi there,
Being on a single product should highlight the Shop menu item by default as long as the Shop menu item is pointing to the standard product archives.
It is not pointing to the standard product archives thats why I asked.
For some reason can't find the place to post you a link in the initial post.