Skip to content

Instantly share code, notes, and snippets.

@marcwiest
Forked from billerickson/functions.php
Last active April 13, 2020 05:35
Show Gist options
  • Save marcwiest/8046153 to your computer and use it in GitHub Desktop.
Save marcwiest/8046153 to your computer and use it in GitHub Desktop.
<?php
wp_nav_menu( array(
'theme_location' => 'mobile',
'walker' => new Walker_Nav_Menu_Dropdown(),
'items_wrap' => '<div class="mobile-menu"><form><select onchange="if (this.value) window.location.href=this.value">%3$s</select></form></div>',
) );
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
}
/**
* Start the element output.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. May be used for padding.
* @param array $args Additional strings.
* @return void
*/
function start_el(&$output, $item, $depth, $args) {
$url = '#' !== $item->url ? $item->url : '';
$output .= '<option value="' . $url . '">' . $item->title;
}
function end_el(&$output, $item, $depth){
$output .= "</option>\n"; // replace closing </li> with the option tag
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment