If you’re not actually using a custom nav menu, why are you not just outputting wp_page_menu()
directly?
Problem
As-implemented, your walker will never be executed (or, more accurately, you can’t control whether or not it is executed), because you’re not passing 'theme_location'
(correct usage) or 'menu'
(sub-optimal usage) to the args array.
Here’s how the wp_nav_menu()
output works:
- WordPress looks for
'menu'
defined in the args array. This value corresponds to the slug of a user-created custom nav menu. So if the user creates a Menu named “Short Header Menu”, the slug would be'short-header-menu'
, which would correspond to'menu' => 'short-header'menu'
. If the menu is found, it is used. - WordPress looks for
'theme_location'
defined in the args array. This value corresponds to the user-defined custom nav menu assigned by the user to the Theme Location. So, if the user creates a Menu named “Short Header Menu”, and assigns it to the (Theme-defined) Theme Location “Primary Menu” (defined by the Theme viaregister_nav_menus( array( 'primary-menu' => "Primary Menu 0 0
), the'short-header-menu'
would be the menu returned. If found, it is used. - If neither
'menu'
nor'theme_location'
is defined the args array, WordPress queries all of the user-created custom nav menus. If the user has created custom nav menus, WordPress uses the first one that has menu items. - If all of the above fail, WordPress outputs the fallback menu, as defined via
'fallback_cb'
.
So, in your example, you’re not passing either 'menu'
or 'theme_location'
to the args array. If the user (e.g. you) hasn’t created any custom nav menus, then WordPress is outputting the 'fallback_cb'
, which by definition is wp_page_menu()
.
Now, wp_nav_menu()
passes its $args
array to the fallback function, but wp_page_menu()
doesn’t accept a 'walker'
parameter. So your custom walker is being ignored.
Solution
- Properly register custom nav menu Theme locations, via
register_nav_menus()
- Create custom nav menus, via
Appearance -> Menus
- Assign a custom nav menu to a registered Theme Location
- Pass the
'theme_location'
parameter to yourwp_nav_menu()
$args
array
Addendum
Note, if the sole purpose of your custom walker is to add the .current-page-item
class, then you don’t need your custom walker at all. The wp_nav_menu()
classes are backwards-compatible with wp_page_menu()
, which means that the .current-page-item
class is already output by wp_nav_menu()
.