guillermorangel
12/16/2013 - 11:17 PM

WordPress Menu as Select Dropdown http://www.billerickson.net/code/wordpress-menu-as-select-dropdown/

<?php

// Nav Menu Dropdown Class
include_once( CHILD_DIR . '/lib/classes/nav-menu-dropdown.php' );

/**
 * Mobile Menu
 *
 */
function be_mobile_menu() {
	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>',
	) );	
}
add_action( 'genesis_before_header', 'be_mobile_menu' );

/**
 * Nav Menu Dropdown
 *
 * @package      BE_Genesis_Child
 * @since        1.0.0
 * @link         https://github.com/billerickson/BE-Genesis-Child
 * @author       Bill Erickson <bill@billerickson.net>
 * @copyright    Copyright (c) 2011, Bill Erickson
 * @license      http://opensource.org/licenses/gpl-2.0.php GNU Public License
 *
 */
 
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
	}
}
?>


<?php 

add_theme_support( 'menus' );

function register_my_menus() {
    register_nav_menus(
      array(
        'main-nav' => 'Main Navigation'
      )
    );
} add_action( 'init', 'register_my_menus' );

if ( has_nav_menu( 'main-nav' ) ) {
  wp_nav_menu(
    array( 
              'menu' => 'main-nav',
         'container' => false,
        'items_wrap' => '<select id="drop-nav"><option value="">Select a page...</option>%3$s</select>',
    'theme_location' => 'main-nav',
  'show_description' => false,
           'walker'  => new Walker_Nav_Menu_Dropdown()
    )
  );
}


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;

      $output .= $indent . ' id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';  
      $attributes  = ! empty( $item->attr_title ) ? ' title="'  . esc_attr( $item->attr_title ) .'"' : '';  
      $attributes .= ! empty( $item->target )     ? ' target="' . esc_attr( $item->target     ) .'"' : '';  
      $attributes .= ! empty( $item->xfn )        ? ' rel="'    . esc_attr( $item->xfn        ) .'"' : '';  
      $attributes .= ! empty( $item->url )        ? ' value="'   . esc_attr( $item->url        ) .'"' : '';  
      
      $item_output .= '<option'. $attributes .'>';  
      $item_output .= $args->link_before .apply_filters( 'the_title', $item->title, $item->ID );  
      $item_output .= '</option>';  
      
      $output .= apply_filters( 'walker_nav_menu_start_el', $item_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
  }
}
?>