Wordpress change wp_nav_menu walker with a custom one

I have a downloaded theme and it where the wp_nav_menu is created it looks something like this:

div class=nv-nav-wrap
    div role=navigation class=?php echo esc_attr( join( ' ', $container_classes ) ); ?
            aria-label=?php esc_attr_e( 'Primary Menu', 'neve' ); ?

        ?php
        echo wp_nav_menu(
            [
                'theme_location' = 'primary',
                'menu_id'        = $menu_id,
                'menu_class'     = 'primary-menu-ul nav-ul' . $additional_menu_class,
                'container'      = 'ul',
                'walker'         = '\Neve\Views\Nav_Walker',
                'fallback_cb'    = '\Neve\Views\Nav_Walker::fallback',
                'echo'           = false,
            ]
        );
        ?
    /div
/div

I've created a child theme and I want to change the walker of the wp_nav_menu. How can I do that? The new walker's name is Aria walker Nav menu (just for reference)

I've tried putting this in functions.php in the child theme:

    wp_nav_menu( array(
      'theme_location' = 'primary',
      'container'      = false,
      'menu_class'     = 'primary-menu-ul nav-ul',
      'walker'         = new Aria_Walker_Nav_Menu(),
      'items_wrap'     = 'ul id=%1$s class=%2$s%3$s/ul',
    ) );

But it creates a whole new menu. How do I just replace the walker for the original one in the theme

Topic walker navigation Wordpress

Category Web


In general you'd add child substitutions for the relevant templates if possible, as Tom mentions in the comments.

Alternately, you can use a wp_nav_menu_args filter hook to swap in your own arguments prior to WordPress retrieving and rendering the menu:

add_filter( 'wp_get_nav_menu_args', 'wpse406010_primary_nav_menu_args' );

function wpse406010_primary_nav_menu_args( $args ) {
  if( $args[ 'theme_location' ] !== 'primary' || $args[ 'walker' ] !== '\Neve\Views\Nav_Walker' )
    return $args;

  $args[ 'container' ]  = false;
  $args[ 'menu_class' ] = 'primary-menu-ul nav-ul';
  $args[ 'walker' ]     = new Aria_Walker_Nav_Menu();
  $args[ 'items_wrap' ] = '<ul id="%1$s" class="%2$s">%3$s</ul>';

  return $args;
}

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.