Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Custom Walker Class - wp_nav_menu() WordPress

  • SOLVED

Hi There,

I have a custom walker class for displaying a custom sub-navigation. Everything was working fine but now all of a sudden it's no longer working?

I turned on WP_DEBUG and it is giving this error where the nav is supposed to display:

Notice: Undefined variable: item_output in ...../nav-output.php on line 119

I have pasted the entire "nav-output.php" file here:
http://pastie.org/private/h3ppiozv1cnq6arkuco0ww


I greatly appreciate your expert assistance.

Cheers.

Answers (1)

2011-03-22

Peter Michael answers:

You probably have a undefined var in your walker class, more specifically at the 'walker_nav_menu_start_el' point. Can you post your class?


Peter Michael comments:

Gosh, this IS probably you custom walker, right? I'm tired :)


Peter Michael comments:

Try changing

global $wp_query;

to

global $wp_query, $item_output;


WP Answers comments:

Thanks a lot for the quick response. When I make the suggested change it no longer displays the error, but the navigation still does not display.

Also, in case you need it, here is the code within the page template that calls up the menu:


<?php
if (function_exists('wp_nav_menu')) {
$menu_args = array('walker' => new sub_nav_walker());
echo '<div id="sub_nav">';
wp_nav_menu($menu_args);
echo '</div><!-- end sub_nav -->';
};?>



Peter Michael comments:

You could also move

$item_output = $args->before;

to right before

if ($item->menu_item_parent != $parent_item_id) {

Then you don't need the global declaration.

For ex.:


...
$this->found_parents[] = $item->ID;
$item_output = $args->before;
//check if the item_parent matches the current item_parent
if ($item->menu_item_parent != $parent_item_id) {
...


Peter Michael comments:

You have no other errors? Just a blank output?


WP Answers comments:

Hey,

The 2nd solution also gave blank output.

I figured it out though! :)

I changed the call to the nav within the template. I realized I wasn't telling it which nav to pull in.... (long day) ;)

Anyway thanks so much for your help.

I added back your code to get rid of the error:


global $wp_query, $item_output;



And I changed the call to the nav within the template to the code below. All is well!


<?php if (function_exists('wp_nav_menu')) {
echo '<div id="sub_nav">';
wp_nav_menu( array(
'container' =>false,
'theme_location' => 'Primary Navigation',
'sort_column' => 'menu_order',
'menu_class' => '',
'echo' => true,
'before' => '',
'after' => '',
'link_before' => '',
'link_after' => '',
'depth' => 0,
'walker' => new sub_nav_walker())
);
echo '</div><!-- end sub_nav -->';} ?>