Using Different Walker Menu Classes for Header and Footer in WordPress

Different Walker menu output for the header and footer in WordPress

Header and footer menus often need different markup: a dropdown with Bootstrap classes in the header, a flat list in the footer. If you already use a custom Walker_Nav_Menu, one Walker class can serve both, as long as it knows which menu location it is rendering.

Problem: The header and footer menus need different structure or classes. How do you check the menu location inside a Walker and change the output depending on it?

Solution: Pass the menu location to the Walker as a constructor argument, or read it from the $args that WordPress passes to every Walker method.

Step 1. Register the menu locations

In functions.php:

register_nav_menus( array(
    'primary'   => __( 'Primary Navigation', 'theme-name' ),
    'secondary' => __( 'Footer Navigation', 'theme-name' ),
) );

Step 2. Call the menu with the Walker

For example, in header.php:

if ( has_nav_menu( 'primary' ) ) {
    wp_nav_menu( array(
        'container'      => false,
        'theme_location' => 'primary',
        'menu_id'        => 'nav',
        'menu_class'     => 'nav navbar-nav',
        'items_wrap'     => '<ul id="%1$s" class="%2$s">%3$s</ul>',
        'walker'         => new Custom_Walker_Nav_Menu( 'primary' ),
    ) );
}

Step 3. Use the location in the Walker

The Walker stores the location from the constructor. This example builds the default markup with the parent class and only changes the item classes per location:

class Custom_Walker_Nav_Menu extends Walker_Nav_Menu {
    public $menu_location = 'primary';

    public function __construct( $menu_location = 'primary' ) {
        $this->menu_location = $menu_location;
    }

    public function start_el( &$output, $item, $depth = 0, $args = null, $id = 0 ) {
        if ( 'primary' === $this->menu_location ) {
            // Header: Bootstrap classes, dropdown on items with children.
            $item->classes[] = 'nav-item';
            if ( in_array( 'menu-item-has-children', (array) $item->classes, true ) ) {
                $item->classes[] = 'dropdown';
            }
        } else {
            // Footer: flat list.
            $item->classes[] = 'footer-menu__item';
        }

        parent::start_el( $output, $item, $depth, $args, $id );
    }
}

If you need the menu object itself (its name or ID), get it from the location:

$locations = get_nav_menu_locations();
$menu      = wp_get_nav_menu_object( $locations[ $this->menu_location ] );

Without the constructor

WordPress passes the wp_nav_menu() arguments to every Walker method in $args, so the location is also available as $args->theme_location. Then the Walker needs no constructor at all, and you can use new Custom_Walker_Nav_Menu() in both places.

NOTE: If the markup differs a lot between locations, two separate Walker classes are easier to maintain than one class full of conditions.

Sources:

  1. https://wordpress.stackexchange.com/questions/256868/walker-nav-menu-put-custom-code-between-a-particular-li