logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$4
nav menu walker as select dropdown

this code nearly does what I want, but it indents subpages inside the parents <option> tag which causes html validation errors
I think I need to set $to_depth to -1, but can't figure it out.


function golden_mobile_menu_secondary(){
wp_nav_menu(array(
'theme_location' => 'secondary', // your theme location here
'walker' => new Walker_Nav_Menu_Dropdown(),
'items_wrap' => '<select id="sec-selector" name="sec-selector" onchange="location.href = document.getElementById(\'sec-selector\').value;">%3$s</select>',
'container_id' => 'mobile_menu_secondary'
));
}

class Walker_Nav_Menu_Dropdown extends Walker_Nav_Menu{
var $to_depth = -1;
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;

parent::start_el(&$output, $item, $depth, $args);

// no point redefining this method too, we just replace the li tag...
$output = str_replace('<li', '<option value="' . $item->url . '"', $output);
}

function end_el(&$output, $item, $depth){
$output .= "</option>\n"; // replace closing </li> with the option tag
}
}

This question has been answered.

paul de wouters | 10/04/11 at 2:42pm Edit


(2) Possible Answers Submitted...

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    10/04/11
    2:45pm
    John Cotton says:

    My guess is that this line needs changing:

    $item->title = str_repeat("&nbsp;", $depth * 4).$item->title;


    Try
    $item->title = $item->title;


    You might also want to remove the '\t' lines as these are just added unnecessary tabs.

    Previous versions of this answer: 10/04/11 at 2:45pm

  • avatar
    Last edited:
    10/04/11
    6:05pm
    Jurre Hanema says:

    Try this!


    class Walker_Nav_Menu_Dropdown extends Walker_Nav_Menu{

    var $to_depth = -1;

    function start_lvl(&$output, $depth){

    $output .= '</option>';

    }



    function end_lvl(&$output, $depth){

    $indent = str_repeat("\t", $depth); // don't output children closing tag

    }



    function start_el(&$output, $item, $depth, $args){
    $indent = ( $depth ) ? str_repeat( "&nbsp;", $depth * 4 ) : '';

    $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 ) . '"' : '';

    $value = ' value="'. $item->url .'"';

    $output .= '<option'.$id.$value.$class_names.'>';

    $item_output = $args->before;
    $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;

    $output .= $indent.$item_output;
    }



    function end_el(&$output, $item, $depth){

    if(substr($output, -9) != '</option>')
    $output .= "</option>"; // replace closing </li> with the option tag

    }

    }

    • 10/04/11 7:39pm

      paul de wouters says:

      thanks that seems to work! good job

This question has expired.



paul de wouters voted on this question.



Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.