Keith Petrillo
Keith Petrillo

Reputation: 161

WordPress nav_menu_link_attributes Not Working

I am attempting to add a data attribute to all menu items, but it's simply not working. I am using wp_nav_menu to call my menu walker as well.

function menu_anchor_attributes ( $atts, $item, $args ) {
    $atts['data-menuanchor'] = $item->attr_title;
return $atts;
}
add_filter( 'nav_menu_link_attributes', 'menu_anchor_attributes', 10, 3 );

Upvotes: 1

Views: 1230

Answers (1)

MacK
MacK

Reputation: 2191

After seven years @nicola gave out a good explanation here: https://wordpress.stackexchange.com/a/328228/73755

The filter nav_menu_link_attributes only works on menus that are created in wp-admin. By default wp_nav_menu returns the published pages even if you did not create a menu on the backend, but as soon as you click "create menu" on Appearance -> Menus the filter does its job.

Upvotes: 0

Related Questions