phpwordpressdrop-down-menuwp-nav-walker

Adding attribute to bootstrap nav walker ul


Im using a bootstrap nav walker for a wordpress site and trying as im trying to re-create a premium theme (only temporary) so matching their markup to save time.

I need to add a attribute to the <ul> so that its <ul data-plugin="menu">... but i cant see anything which can help me how to do so. I can see how to add attributes to menu links but not the menu itself (without adding it via jQuery etc).

My output for the menu is pretty simple:

<?php
wp_nav_menu( array(
    'menu'       => 'top',
    'depth'      => 2,
    'container'  => false,
    'menu_class' => 'site-menu scrollable-content',
    'walker'     => new wp_bootstrap_navwalker())
);
?>

Solution

  • to add custom attributes on the items container you could specify a custom template in items_wrap

    'items_wrap' => '<ul data-plugin="menu" id="%1$s" class="%2$s">%3$s</ul>'
    

    resulting in this call to wp_nav_menu

    <?php
    wp_nav_menu( array(
        'menu'       => 'top',
        'depth'      => 2,
        'container'  => false,
        'menu_class' => 'site-menu scrollable-content',
        'walker'     => new wp_bootstrap_navwalker(),
        'items_wrap' => '<ul data-plugin="menu" id="%1$s" class="%2$s">%3$s</ul>'
    );
    ?>
    

    items_wrap is used like this to build the final nav menu:

    $nav_menu .= sprintf( $args->items_wrap, esc_attr( $wrap_id ), esc_attr( $wrap_class ), $items );