Skip to content

Instantly share code, notes, and snippets.

@keccers
Created November 12, 2013 19:04
Show Gist options
  • Save keccers/7436790 to your computer and use it in GitHub Desktop.
Save keccers/7436790 to your computer and use it in GitHub Desktop.
Walker! Throw in functions.php
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 . ' <select 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
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment