[wp-trac] [WordPress Trac] #18842: wp_nav_menu confuses new developers when it falls through to page listing

WordPress Trac noreply at wordpress.org
Tue Sep 19 22:18:02 UTC 2017


#18842: wp_nav_menu confuses new developers when it falls through to page listing
--------------------------+------------------------------
 Reporter:  dd32          |       Owner:
     Type:  defect (bug)  |      Status:  closed
 Priority:  normal        |   Milestone:  Awaiting Review
Component:  Menus         |     Version:  3.2
 Severity:  normal        |  Resolution:  invalid
 Keywords:  needs-patch   |     Focuses:
--------------------------+------------------------------
Changes (by joyously):

 * status:  new => closed
 * resolution:   => invalid


Comment:

 I was attempting to fix this for a theme, by using a filter on
 'page_menu_args', but wp_page_menu not only ignores the args, but has
 different meanings for some and sets others itself.
 So this code //almost// gets it, but not handling the case where the
 menu_id is empty.

 {{{
 function my_page_menu_args( $args )  {
   list( $before, $after ) = explode('%3$s', $args['items_wrap'] );
   $wanted = array(
     'menu_id'    => $args['container_id'],
     'menu_class' => $args['container_class'],
     'before'     => sprintf( $before, esc_attr( $args['menu_id'] ),
 esc_attr( $args['menu_class'] ) ),
     'after'      => $after,
   );
   unset( $args['fallback_cb'] );  // to bypass wp_page_menu setting args
 for us
   return wp_parse_args( $wanted, $args );
 }
 }}}
 One of the problems is that wp_page_menu checks to see if it's a fallback,
 and then sets the args a certain way. But it always returns a container,
 even though wp_nav_menu checks if container is empty .

 The sequence in the code at this time is:
 {{{
 wp_nav_menu sets default args
 wp_nav_menu combines defaults with passed args
 wp_nav_menu filters the args
 if no menu, return fallback passing filtered args

   wp_page_menu sets default args
   wp_page_menu combines defaults with passed args
   wp_page_menu filters the args
   wp_page_menu sets the 'container' to a div if it's empty
   wp_page_menu sets 'before' and 'after' to plain `ul` if it's fallback
   wp_page_menu wraps the menu with 'before' and 'after'
   wp_page_menu wraps the menu with 'container' using 'menu_id' and
 'menu_class'
   wp_page_menu filters the entire menu before returning it

 // else the menu exists
 wp_nav_menu uses 'container_id' and 'container_class' for 'container'
 wp_nav_menu adjusts menu for child classes
 wp_nav_menu filters the menu objects and resulting menu several ways
 wp_nav_menu wraps the menu with 'items_wrap' using built 'menu_id' and
 'menu_class'
 wp_nav_menu wraps the menu with built 'container'
 wp_nav_menu filters the entire menu before returning it
 }}}

 What I would suggest is a small change to wp_page_menu to handle the args
 like wp_nav_menu when it checks for fallback.
 @SergeyBiryukov, can you take a look at this?

 Other tickets that are all pretty much the same thing:
 #16679: wp_nav_menu treats menu_class differently for fallback
 #14614: Can't remove container in wp_nav_menu
 #18232: wp_nav_menu - Setting walker parameter affects fallback_cb

--
Ticket URL: <https://core.trac.wordpress.org/ticket/18842#comment:6>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list