menus - WordPress nav_menu_link_attributes Not Working

admin2025-06-03  3

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 );

I am using JointsWP as my framework, which includes this walker:

// The Top Menu
function joints_top_nav() {
     wp_nav_menu(array(
        'container' => false,                           // Remove nav container
        'menu_class' => 'horizontal menu',       // Adding custom nav class
        'items_wrap' => '<ul id="%1$s" class="%2$s" data-responsive-menu="accordion medium-dropdown">%3$s</ul>',
        'theme_location' => 'main-nav',                 // Where it's located in the theme
        'depth' => 5,                                   // Limit the depth of the nav
        'fallback_cb' => false,                         // Fallback function (see below)
        'walker' => new Topbar_Menu_Walker()
    ));
} 

// Big thanks to Brett Mason () for the awesome walker
class Topbar_Menu_Walker extends Walker_Nav_Menu {
    function start_lvl(&$output, $depth = 0, $args = Array() ) {
        $indent = str_repeat("\t", $depth);
        $output .= "\n$indent<ul class=\"menu\">\n";
    }
}

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 );

I am using JointsWP as my framework, which includes this walker:

// The Top Menu
function joints_top_nav() {
     wp_nav_menu(array(
        'container' => false,                           // Remove nav container
        'menu_class' => 'horizontal menu',       // Adding custom nav class
        'items_wrap' => '<ul id="%1$s" class="%2$s" data-responsive-menu="accordion medium-dropdown">%3$s</ul>',
        'theme_location' => 'main-nav',                 // Where it's located in the theme
        'depth' => 5,                                   // Limit the depth of the nav
        'fallback_cb' => false,                         // Fallback function (see below)
        'walker' => new Topbar_Menu_Walker()
    ));
} 

// Big thanks to Brett Mason (https://github/brettsmason) for the awesome walker
class Topbar_Menu_Walker extends Walker_Nav_Menu {
    function start_lvl(&$output, $depth = 0, $args = Array() ) {
        $indent = str_repeat("\t", $depth);
        $output .= "\n$indent<ul class=\"menu\">\n";
    }
}
Share Improve this question edited Aug 24, 2016 at 18:32 Keith Petrillo asked Aug 24, 2016 at 18:25 Keith PetrilloKeith Petrillo 832 silver badges9 bronze badges 7
  • Could you edit your question ( using the edit link above ) and add in your Walker Class please? It could be that it doesn't call the filter. – Howdy_McGee Commented Aug 24, 2016 at 18:26
  • Of course, I have included the walker. – Keith Petrillo Commented Aug 24, 2016 at 18:33
  • What happens if you put ( ! empty( $item->attr_title ) ) ? $item->attr_title : 'test'; in your attributes filter? I have a suspicion that attr_title is an empty string and the attribute is being ignored because of this. – Howdy_McGee Commented Aug 24, 2016 at 18:38
  • Nothing happens, unfortunately. Not even an empty data-menuanchor attribute displays. – Keith Petrillo Commented Aug 24, 2016 at 19:35
  • Something else is the issue then, if I paste the provided code above into my theme and add the conditional in the comment above - it adds the attribute to the anchor tag as expected. Try removing this functionality from your theme and add it to a WordPress default theme like Twenty Fifteen or something of the sort. If you're able to get it working - from there you'll need to disable all the installed plugins and tear your theme apart until you figure out what is causing the discrepancy. – Howdy_McGee Commented Aug 24, 2016 at 19:39
 |  Show 2 more comments

2 Answers 2

Reset to default 1

Whenever an attribute is empty WordPress filters decide just not to show the attribute so a simple test for this is the following:

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

This way, if a title attribute hasn't been filled into the back-end it will still display the attribute with a value of test.

The fiter 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

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748923685a314857.html

最新回复(0)