0

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

1
  • you'd need to replace the file the original theme uses to display the menu in your child theme with one that is the same but has your new walker. Unfortunately you did not mention any details of where or how the theme does this so it's not possible to say if this would work or not and which file would need to be changed, or if an alternative method is necessary. functions.php is not the place to put wp_nav_menu calls, are you familiar with how child themes templates work?
    – Tom J Nowell
    May 23, 2022 at 17:17

1 Answer 1

0

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;
}
1
  • I didn't find a filter called wp_get_nav_menu_args in the current version of WordPress. Did you mean wp_nav_menu_args?
    – schuhwerk
    Sep 8 at 16:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.