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.
Description Description
Parameters Parameters
- $atts
-
(array) The HTML attributes applied to the menu item's
<a>element, empty strings are ignored.- 'title'
(string) Title attribute. - 'target'
(string) Target attribute. - 'rel'
(string) The rel attribute. - 'href'
(string) The href attribute. - 'aria_current'
(string) The aria-current attribute.
- 'title'
- $item
-
(WP_Post) The current menu item.
- $args
-
(stdClass) An object of wp_nav_menu() arguments.
- $depth
-
(int) Depth of menu item. Used for padding.
Source Source
Changelog Changelog
| Version | Description |
|---|---|
| 4.1.0 | The $depth parameter was added. |
| 3.6.0 | Introduced. |
User Contributed Notes User Contributed Notes
You must log in before being able to contribute a note or feedback.
The default Walker menu does not include a class attribute on the anchor (
<a>) element, but one can easily be added using this filter.function add_class_to_all_menu_anchors( $atts ) { $atts['class'] = 'menu-item-anchor'; return $atts; } add_filter( 'nav_menu_link_attributes', 'add_class_to_all_menu_anchors', 10 );You might notice there that I am only passing one parameter (
$atts) to the function. This is because we only need the$attsin this case and there is no need to pass other parameters. Alternatively, let’s say we wanted to add a class to all anchors which are not top level:function add_class_to_non_top_level_menu_anchors( $atts, $item, $args, $depth ) { if ( 0 !== $depth ) { $atts['class'] = 'menu-item-anchor-non-top'; } return $atts; } add_filter( 'nav_menu_link_attributes', 'add_class_to_non_top_level_menu_anchors', 10, 4 );In this case, we need to pass all four parameters because we are using the
$depthparameter.