Make WordPress Core

Opened 13 years ago

Last modified 6 years ago

#18232 new defect (bug)

wp_nav_menu - Setting walker parameter affects fallback_cb

Reported by: bryanjwatson's profile bryanjwatson Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.2
Component: Menus Keywords: has-patch needs-testing needs-refresh
Focuses: Cc:

Description

When using the walker parameter with wp_nav_menu and there is no menu present, wp_nav_menu falls back by default (set with fallback_cb) to wp_page_menu. It appears that the set walker also affects wp_page_menu, which in most cases breaks the output.

wp_nav_menu usage:

<?php wp_nav_menu(array('walker'=> new Walker_Nav_Menu)); ?>

Output when no menu is present (falling back on wp_page_menu):

// Formatted for readability
<div class="menu">
	<ul>
		<li id="menu-item-1" class="menu-item-1"><a></a></li>
		<li id="menu-item-2" class="menu-item-2"><a></a></li>
		<li id="menu-item-3" class="menu-item-3"><a></a></li>
	</ul>
</div>

Expected output:

// Formatted for readability
<div class="menu">
	<ul>
		<li class="page_item page-item-1"><a href="/first/" title="First">First</a></li>
		<li class="page_item page-item-2"><a href="/second/" title="Second">Second</a></li>
		<li class="page_item page-item-3"><a href="/third/" title="Third">Third</a></li>
	</ul>
</div>

As you can see, the output from wp_page_menu is getting filtered through the walker, but since the walker was designed for menus specifically, it renders the default output from wp_page_menu useless (empty tags..etc).

Attachments (2)

18232.patch (870 bytes) - added by SergeyBiryukov 13 years ago.
18232_v2.patch (1.5 KB) - added by bryanjwatson 13 years ago.

Download all attachments as: .zip

Change History (9)

#1 @SergeyBiryukov
13 years ago

  • Keywords has-patch added; needs-patch removed

#2 @bryanjwatson
13 years ago

  • Keywords needs-testing added
  • Summary changed from wp_nav_menu - Setting walker parameter affects fallback_cb (wp_page_menu) to wp_nav_menu - Setting walker parameter affects fallback_cb

Simple patch, but unfortunately it does not account for the scenario when a function other than wp_page_menu is set as the fallback_cb parameter.

For example, this would still break:

<?php wp_nav_menu(array('fallback_cb'=>'wp_list_categories','walker'=>new Walker_Nav_Menu)); ?>

I believe the issue resides in the fact that the fallback function is inheriting the $args passed to wp_nav_menu.

Solution: Stop the $args inheritance on the fallback function, and create a new wp_nav_menu parameter to provide custom fallback args if needed. See patch 18232_v2.

#3 @markoheijnen
13 years ago

  • Cc marko@… added

Is it really necessary to have special arguments for the fallback only for the walker? I rather would create a parameter only for setting the walker for the fallback.

#4 @BenjaminRMueller
12 years ago

  • Cc BenjaminRMueller added

#6 @kwight
11 years ago

  • Cc kwight@… added

#7 @chriscct7
9 years ago

  • Keywords needs-refresh added
  • Version changed from 3.2.1 to 3.2
Note: See TracTickets for help on using tickets.