Yes, there is, and the hook is nav_menu_link_attributes
:
apply_filters( 'nav_menu_link_attributes', array $atts, WP_Post $item, stdClass $args, int $depth )
Filters the HTML attributes applied to a menu item’s anchor element.
For example, this adds the nav-link
class to the <a>
tag only if the theme location ($args->theme_location
) is exactly my-location
:
add_filter( 'nav_menu_link_attributes', 'my_nav_menu_link_attributes', 10, 3 );
function my_nav_menu_link_attributes( $atts, $item, $args ) {
if ( 'my-location' === $args->theme_location ) {
// Get existing classes, if any.
$class = $atts['class'] ?? '';
// Now add your custom class(es).
$atts['class'] = "$class nav-link";
}
return $atts;
}
Related Posts:
- On which hook should I be calling register_nav_menu(s)?
- Adding custom text in items titles from wp_nav_menu()
- Why is ‘nav_menu_item_args’ filter’s $item argument empty?
- Third level navigation class
- Hook for number of items in Appearance > Menus > CustomPostType > View All [duplicate]
- Filter for Admin Nav Menus Drop Down
- current-menu-item class not working properly
- Trying to add a custom social icon to a Twenty Twenty-One child theme
- Add a counter beside menu item label
- Removing Submenu from Menu
- insert an HTML element if there is a submenu only
- display cart information within li of cart menu item
- How to handle change the appearance of ‘categories’ box on dashboard?
- How to modify menu text in callback function of ‘walker_nav_menu_start_el’ hook
- Get_avatar filter hook not displaying custom avatar image on frontend
- Displaying a Custom Avatar in WP Bootstrap Navwalker Menus in WordPress
- Add button and title on submenu WordPress
- Using a menu walker add a custom item at the end of the menu’s items
- Add settings to menu items in the Customizer
- Generate a Menu that Displays Child Pages using wp_list_pages() with the New Menu Functionality in WordPress 3.0?
- WordPress Shortcode in Menu Item Title
- Calling the Menu Title within wp_nav_menu array function
- How to hide an item from a menu to logged out users (without a plugin)
- Menu items (all menus) being deleted randomly on their own [duplicate]
- How to apply a class to the second word in menu list item?
- Put a wp_nav_menu inside another one
- WordPress wp_nav_menu within iFrame
- Generate a Menu that displays all child pages of top level parent
- Check if wp_nav_menu items have submenus
- start_lvl on Walker is not working
- How to get Menu Navigation Labels to output HTML
- wp_nav_menu doesn’t generate parent/ancestor classes
- WP_nav_menu fallback not working
- How to conditionally register nav menus with advanced custom fields?
- Pending Custom Link in Menu
- Change where navigation bar links?
- Social Links Menu displays icon instead of text (twentyfifteen) [closed]
- has_children in custom nav_walker
- Does wp_nav_menu use the gettext filter?
- How to add #navbar to all page links?
- How to add wechat (weixin) link to navigation menu?
- Link that refers to a menu item with dynamically generated URL
- Change an li class name in a wordpress custom menu walker
- Custom walker nav – Add sequential number as class
- WordPress Custom Menus Issue
- Side Menu Icon Expandable
- wordpress responsive menu not dropping down
- Make nav-menus.php available for editor
- how to add custom wp_nav_menu class css selector to ul and a tags?
- Need Random Category Nav Menu
- Split into two seperate unordered lists with custom walker
- edit user menu item
- Excluding a media query from specific pages
- Use dashicons in menu items?
- Menu Arrow for Child Element
- Custom menu with different behavior for every item
- Responsive dropdown menu with smaller screen sizes
- set certain item in nav walker to active when on archive pages or singles
- wp_nav_menu() | Outputting my own custom code using built in features
- How to reference argument data for wp_nav_menu() from functions.php
- Creating menu always gives error A name is required for this term
- Showing the same menu in multiple location
- Creating custom menu with custom themes
- Expected snippet value not showing
- Wp Nav Menu div containers
- wp_nav_menu with WP as pure CMS
- Add posts from a category to menu with all posts in it as sub menu
- Possible to append class to nav menu added to widget area?
- Page dependent sub-menu?
- How can I creat custom menu with bootstrap
- Removing wp admin menu item on top bar of admin backend
- Error: Declaration of MyClass::start_lvl() should be compatible with that of Walker_Nav_Menu::start_lvl()
- WordPress nav-menus not finding page
- Restrict user to edit only edit a certain menu
- Primary menu items are dissapearing
- Where is the source code in the console coming from?
- Migrate old blogroll to menus automatically
- Getting parent object_id of child menu items in WordPress menu
- How to remove a sub-menu using walker hooks/filters without relying on a custom walker?
- Calling wordpress menu outside ignores custom menu item
- Removing menu item doesn’t get removed on edit post
- change link of menu particular tab
- Next page link via the custom menu
- How can I make this custom design menu work in wordpress?
- Active menu item / category
- Add different span class to wp nav menu
- Top menu link anchor href value change after online server?
- Menu not always using correct link
- Custom Menus and taxonomies
- wp_nav_menu doesn’t show on index page
- How to add posts with thumbnails to my dropdown menu?
- Automatically add new posts to a specific menu name
- can anyone help me converting my static menu to wp_nav_menu I’am trying to add static menu item to WordPress Menu
- Elegant way of adding an CTA to a menu
- Two Homes In Navigation Menu
- Theme supports only one menu. I need to add more – but how? [closed]
- how to add permalink/slug to wordpress custom link menu item
- Using walker to display all menu branches at once?
- How to add a class directly on a unordered list element that is a primary navigation
- How to change WP menu item type?