Skip to content

Instantly share code, notes, and snippets.

@danalmeida
Created August 12, 2013 14:51
Show Gist options
  • Save danalmeida/6211490 to your computer and use it in GitHub Desktop.
Save danalmeida/6211490 to your computer and use it in GitHub Desktop.
Generates Wordpress wp_nav_menu() as a select field, for small screens
<div class="drop-down">
<?php
// Get the nav menu based on $menu_name (same as 'theme_location' or 'menu' arg to wp_nav_menu)
// This code based on wp_nav_menu's code to get Menu ID from menu slug
if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $menu_name ] ) ) {
$menu = wp_get_nav_menu_object( $locations[ $menu_name ] );
$menu_items = wp_get_nav_menu_items($menu->term_id);
$wrapper = 'select';
$menu_list = '<' . $wrapper . ' id="menu-' . $menu_name . '">';
$menu_list .= '<option value="">Go to&hellip;</option>';
foreach ( (array) $menu_items as $key => $menu_item ) {
$title = $menu_item->title;
$url = $menu_item->url;
$menu_list .= '<option value="' . $url . '">' . $title . '</option>';
}
$menu_list .= '</' . $wrapper . '>';
} else {
$menu_list = '<' . $wrapper . '><li>Menu "' . $menu_name . '" not defined.</li></' . $wrapper . '>';
}
// $menu_list now ready to output
echo $menu_list;
?>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment