- This topic has 32 replies, 2 voices, and was last updated 4 months, 1 week ago by
David.
-
AuthorPosts
-
October 2, 2017 at 12:33 pm #396076
Harris
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!
October 2, 2017 at 1:15 pm #396114Leo
StaffCustomer SupportHi 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.
October 2, 2017 at 2:42 pm #396170Harris
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 );
October 2, 2017 at 7:58 pm #396256Tom
Lead DeveloperLead DeveloperThat code is perfect ๐
October 3, 2017 at 12:27 am #396352Harris
Any chance ot get some help to adopt it in GP?
October 3, 2017 at 8:26 am #396660Leo
StaffCustomer SupportDid you try adding it or not yet?
If not try one of the methods here: https://docs.generatepress.com/article/adding-php/
October 3, 2017 at 8:37 am #396685Harris
Hello,
I am trying to find what the needed parameters are in order to make it work.October 3, 2017 at 9:02 am #396719Tom
Lead DeveloperLead DeveloperIs “News” a category name, or just all of your blog posts?
October 3, 2017 at 9:07 am #396723Harris
The category name is “news” and I want to “asign” it in the menu item named “NEA”
October 3, 2017 at 6:03 pm #396963Tom
Lead DeveloperLead DeveloperIn 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 );
October 4, 2017 at 12:21 am #397105Harris
Thanks a lot Tom!
For some reason I thought I had to change more parameters than the posts category and the menu item name.October 4, 2017 at 9:29 am #397378Tom
Lead DeveloperLead DeveloperNo problem! ๐
December 15, 2019 at 11:43 pm #1105442Harris
Hello!
Can this be modified to cover single product pages (woocommerce) instead of single posts pages?
Thanks
December 16, 2019 at 9:47 am #1106070Tom
Lead DeveloperLead DeveloperHi 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.
December 16, 2019 at 10:42 am #1106109Harris
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. -
AuthorPosts
- You must be logged in to reply to this topic.