Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm using the following walker to create a dropdown menu that I will use on a mobile version of a project I'm working on... it works fine, however, when I select the page, it doesn't load the page, what am I missing?

class Walker_Nav_Menu_Dropdown extends Walker_Nav_Menu{
    function start_lvl(&$output, $depth){
      $indent = str_repeat("\t", $depth); // don't output children opening tag (`<ul>`)
    }

    function end_lvl(&$output, $depth){
      $indent = str_repeat("\t", $depth); // don't output children closing tag
    }

    function start_el(&$output, $item, $depth, $args){
      // add spacing to the title based on the depth
      $item->title = str_repeat("&nbsp;", $depth * 4).$item->title;

      parent::start_el(&$output, $item, $depth, $args);

      // no point redefining this method too, we just replace the li tag...
      $output = str_replace('<li', '<option', $output);
    }

    function end_el(&$output, $item, $depth){
      $output .= "</option>\n"; // replace closing </li> with the option tag
    }
}

Thanks in advance.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

You have to tell the browser to change the address in some way.

A simple javascript solution is to put the item URL into the value attribute, and trigger the address change when the option is selected.

Replace:

$output = str_replace('<li', '<option', $output);

With:

$output = str_replace(
 '<li',
 '<option value="'.$item->url.'" onchange="window.location.href=this.value"',

 $output);
share|improve this answer
1  
And don't forget a submit button for users without JavaScript. –  toscho Apr 9 '12 at 21:53
    
thanks a ton! worked like charm –  andresmijares25 Apr 10 '12 at 16:22
    
I just realized that I did not make it work exactly as you said, just in case anyone else needs 1, the onchange event has to be placed it in the select, not the option! everything else is perfect –  andresmijares25 Apr 13 '12 at 18:40
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.