I'm trying to replace the walker argument from new My_Walker_Nav_Menu() to a string 'My_Walker_Nav_Menu' in this wp_nav_menu call (which works as is)
wp_nav_menu( array( 'theme_location' => 'header_nav', 'menu_class' => 'main-menu', 'container' => '', 'fallback_cb' => false, 'walker' => new My_Walker_Nav_Menu() ) ); When I change it to a string I'm getting this error Fatal error: Using $this when not in object context
The class is basic:
class My_Walker_Nav_Menu extends Walker_Nav_Menu { function start_lvl(&$output, $depth = 0, $args = Array()) { $indent = str_repeat("\t", $depth); if('header_nav' == $args->theme_location ){ $output .='<span class="toggle-submenu fa fa-angle-down"></span>'; } $output .= "\n$indent<ul class=\"sub-menu\">\n"; } } I need to do this because apparently Customizer's partial refresh doesn't work with custom walkers.
When the Customizer determines it can’t do a partial refresh, it falls back to performing a full page refresh. Conditions for why a menu change will get a full page refresh include:
...
- or if wp_nav_menu() is called with with a walker object instance, as opposed to a class name string;
Somebody else had this issue (mentioned in a comment) but didn't get an answer. I've searched through the docs but can't seem to find anything.
Any help is much appreciated.
wp_nav_menu(), andwalk_nav_menu_tree()that it uses, I don't see how passing a class name string can even work.Walker_Nav_MenuextendsWalker, which is riddled with$this, so cannot be called statically. As far as I can tell, the only way you could use a class name string as the walker argument forwp_nav_menuis if the class did not extendWalker_Nav_MenuorWalkerand instead re-implemented all its methods statically.wp_nav_menustill only says thatwalkeris for an "Instance of a custom walker class. Default empty." Key word there being "instance".