Skip to content

Instantly share code, notes, and snippets.

Created November 7, 2011 03:55
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 anonymous/1344147 to your computer and use it in GitHub Desktop.
Save anonymous/1344147 to your computer and use it in GitHub Desktop.
class select_menu_walker extends Walker_Nav_Menu{
function start_lvl(&$output, $depth) {
$indent = str_repeat("\t", $depth);
$output .= "";
}
function end_lvl(&$output, $depth) {
$indent = str_repeat("\t", $depth);
$output .= "";
}
function start_el(&$output, $item, $depth, $args) {
global $wp_query;
$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
$class_names = $value = '';
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$classes[] = 'menu-item-' . $item->ID;
$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
$class_names = ' class="' . esc_attr( $class_names ) . '"';
$id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
$id = strlen( $id ) ? ' id="' . esc_attr( $id ) . '"' : '';
//check if current page is selected page and add selected value to select element
$selc = '';
$curr_class = 'current-menu-item';
$is_current = strpos($class_names, $curr_class);
if($is_current === false){
$selc = "";
}else{
$selc = "selected ";
}
$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 ) ? ' href="' . esc_attr( $item->url ) .'"' : '';
$sel_val = ' value="' . esc_attr( $item->url ) .'"';
//check if the menu is a submenu
switch ($depth){
case 0:
$dp = "";
break;
case 1:
$dp = "-";
break;
case 2:
$dp = "--";
break;
case 3:
$dp = "---";
break;
case 4:
$dp = "----";
break;
default:
$dp = "";
}
$output .= $indent . '<option'. $sel_val . $id . $value . $class_names . $selc . '>'.$dp;
$item_output = $args->before;
//$item_output .= '<a'. $attributes .'>';
$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
//$item_output .= '</a>';
$item_output .= $args->after;
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
}
function end_el(&$output, $item, $depth) {
$output .= "</option>\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment