Skip to content

Instantly share code, notes, and snippets.

@besimhu
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save besimhu/af49a1c62a20fb8a2632 to your computer and use it in GitHub Desktop.
Save besimhu/af49a1c62a20fb8a2632 to your computer and use it in GitHub Desktop.
Sometimes a standard navigation just will not do, in that case we can use a select dropdown to rely on native phone UI.
<?php
/**
* Select Menu Menu Walker
* Add to functions.php file
*/
class SelectBox_Menu_Walker extends Walker_Nav_Menu {
function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
global $wp_query;
$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$selected = in_array('current-menu-item',$classes) ? 'selected="selected"' : '';
$output .= '<option '.$selected.' value="'.$item->url.'">';
$output .= $item->title;
}
function end_el( &$output, $item, $depth = 0, $args = array() ) {
$output .= "</option>";
}
}
<?php
wp_nav_menu(array(
'container' => '',
'menu' => 'Menu Name',
'items_wrap' => '<div class="show-desktop"><ul id="%1$s" class="%2$s">%3$s</ul></div>'
));
wp_nav_menu(array(
'container' => '',
'menu' => 'Menu Name',
'walker' => new SelectBox_Menu_Walker(),
'items_wrap' => '<div class="show-mobile"><form><select onchange="if (this.value) window.location.href=this.value">%3$s</select></form></div>'
));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment