Error “Trying to get property of non-object” with Custom Walker for wp_nav_menu

I get this error when there are no menus defined or no menus set for the location at Appearance->Menus. When that occurs wp_nav_menu uses a page walker fallback. The fallback (default) for wp_nav_menu is wp_walker_page which uses wp_page_menu which uses wp_list_pages which uses walk_page_tree which uses Walker_Page not Walker_Nav_Menu. And apparently the two walkers are … Read more

Using a menu walker add a custom item at the end of the menu’s items

You don’t need a walker in this case. A filter called wp_nav_menu_items is available. It allows you to edit the list items of a menu. Just append your own list item with search field. add_filter( ‘wp_nav_menu_items’, ‘add_search_to_nav’, 10, 2 ); function add_search_to_nav( $items, $args ) { $items .= ‘<li>SEARCH</li>’; return $items; } Note: if you … Read more

Add a custom walker to a menu created in a widget

If you look at implementation of WP_Nav_Menu_Widget class you will see the following code: function widget($args, $instance) { // Get menu $nav_menu = ! empty( $instance[‘nav_menu’] ) ? wp_get_nav_menu_object( $instance[‘nav_menu’] ) : false; if ( !$nav_menu ) return; $instance[‘title’] = apply_filters( ‘widget_title’, empty( $instance[‘title’] ) ? ” : $instance[‘title’], $instance, $this->id_base ); echo $args[‘before_widget’]; if … Read more

Custom Nav walker display current menu item children, or siblings on no children

This is the walker I used to display only children of the current menu item. Or the menu items siblings if it doesn’t have any children of its own. There are comments throughout the class explaining each section <?php class SH_Child_Only_Walker extends Walker_Nav_Menu { private $ID; private $depth; private $classes = array(); private $child_count = … Read more

Menu items description? Custom Walker for wp_nav_menu()

You need a custom walker for the nav menu. Basically, you add a parameter ‘walker’ to the wp_nav_menu() options and call an instance of an enhanced class: wp_nav_menu( array ( ‘menu’ => ‘main-menu’, ‘container’ => FALSE, ‘container_id’ => FALSE, ‘menu_class’ => ”, ‘menu_id’ => FALSE, ‘depth’ => 1, ‘walker’ => new Description_Walker ) ); The … Read more