Just intercept the nav classes and search for the current-menu-item class. This will give you the $item it’s associated with.
add_filter( 'nav_menu_css_class', 'get_active_class', 10, 2 );
function get_active_class( $classes, $item )
{
if ( in_array( 'current-menu-item', $classes) ) {
// We found the active class!: $item
}
return $classes;
}
Now just grab your nav menu and apply the needed css classes or style="display:none" to the others.
Related Posts:
- Dynamically exclude menu items from wp_nav_menu
- Add custom menu item using wp_nav_menu_items filter
- Custom search filter causes menu and query_posts problems
- Unregister Nav Menu from Child-Theme
- Allow html tags in WordPress Custom Menus Description Field
- Using filters to change href of nav menu page link
- How to apply a class to the second word in menu list item?
- WordPress nav_menu_link_attributes Not Working
- Is it possible to add a filter for the output of nav-menu items in admin?
- if role is logged in then do something
- Filter menu item to add classes/attributes?
- Edit html for menu items in default frontend menu
- Set Item Order with wp_nav_menu_{$menu->slug}_items
- When at / ( or “home”) my menu doesn’t get a current-menu-item class
- How to add parent element’s title to nav menu’s child?
- Why is ‘nav_menu_item_args’ filter’s $item argument empty?
- Add class to active element and its parent in nav menu
- Removing strip_tags not working on nav_menu_description
- Third level navigation class
- How to add #navbar to all page links?
- add span to top level links of a wordpress menu called “main”
- Debugging – trying to add search box to menus
- wp_list_pages() but only show children on the branch you are on
- From a page, is it possible to find entry’s “menu_item_parent”?
- Add active class to top-level menu items when viewing single for that post type
- add_filter on menu items to add a menu item
- how to disable default menu classes
- Insert other menus as submenus of a main menu
- list category posts under its item in menu
- Prevent menu from loading in a page template [duplicate]
- Insert a div before the wp_nav_menu
- How to change menu structure?
- Secondary Navigation Menudisappeared When using wp_nav_menu_items
- remove specific classes from menu items
- wp_nav_menu action/filter help
- add_filter for where statment issue
- Add a counter beside menu item label
- Add custom link on the fly to nav menu
- menu-item class missing on wordpress menu
- Filter to hook into menu items and add a custom attribute
- if role is logged in then do something
- Possible to append class to nav menu added to widget area?
- Modifying Woocommerce menu based on tags and categories
- Why does WordPress convert absolute URLs to relative URLs for menu items?
- Menu location switcher – example code fails
- Add Featured Image and Title to wp_nav_menu items
- Show thumbnail to category items in wp_nav_menu(), how?
- Add Class to Custom Menu Widget ul
- Avoid changing menu query with suppress_filters => false
- Removing menu item doesn’t get removed on edit post
- Add Protocol to Custom Menus
- How to modify menu text in callback function of ‘walker_nav_menu_start_el’ hook
- Why is WordPress stripping anchor links from mneu item URLs?
- Add button and title on submenu WordPress
- In WordPress, I want to use code to filter out one menu item (page) from displaying when I call wp_nav_menu() function
- Customizing Only a Specific Menu using the “wp_nav_menu_items” Hook?
- How make top level menu item not have link, but have sub-menus that are linked?
- How Does The Walker Class Work?
- Large WP 3.0 menu times out and won’t save
- How to switch between the Primary Menus programmatically?
- Terms with more than one occurrence have current-menu-item class in menu
- How to get Menu Navigation Labels to output HTML
- custom menu not found
- Benefits of using Nav Walker?
- Current menu item
- How to quickly create a dummy menu?
- wp_list_pages: only show subpages on the parent page?
- How do I remove the link element from the current page in a wp_list_pages menu?
- Altering html structure and creating custom menus
- How to construct a custom html for submenus
- JSON file export of nav menu
- Generating wordpress menu – cant understand the config
- SlideDeck 2, make back-end visible for admins only
- Display menu locations in all sites of a multi site
- Making a navigation header menu with bulma, need some advice
- How to retrieve parent menu item name of current submenu item
- WordPress menu performance is slow when lot of items [closed]
- How to remove UL class in wp_nav_menu?
- Adding an external link to wordpress menu
- Why is my array of nav menus returning empty?
- Get the name of menu item with wp_nav_menu
- Can you exclude child pages of a specific parent within a navigation?
- My menu doesn’t fit in the default screen in the mobile version
- get_nva_menu_locations returns only filled ones
- Add attributes to wrapper html generated by wp_nav_menu
- How can I click the parent menu then redirect to a special URL?
- Stuck while adding bootstrap navwalker class
- use value returned from shortcode as menu item
- How do I display menu in header without loosing styles?
- Create a menu item with a blank title
- Navigation bar is on live site but nothing under menu so can’t add in a page
- Displaying a part of a menu containing the page the user is currently viewing and only it’s immediate children
- WordPress adding Question Mark to URL when using Hashtags in Nav
- wp_nav_menu list children if on parent page but sibling if on sibling page
- Programatically retreive page content from its template page
- Custom menus not showing
- ID of parent Menu
- Nav menu custom html:
- how to mage the submenu open on wordpress active page
- Unable to order my menu by dragging