javascriptphpwordpresswp-nav-walker

walker menu, get parent element in a dropdown and child element in another element


here is my situation, I'm trying to have a custom wordpress menu:

so far my walker is like this:

  class My_Walker_Nav_Menu_language extends Walker_Nav_Menu {

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$indent"; // replace closing </li> with the option tag
}

}

The problem being, it's adding parent and children element in the same select dropdown instead of two separates one . .

how can this be achieve ?

It will be super to have some help with that,

Thanks

-- EDIT --

Example:

If i have a menu like:

<ul id="menu-network-menu" class="menu">
<li id="menu-item-537" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children YO menu-item-537">
    <a href="#">Europe</a>
    <ul class="sub-menu">
        <li id="menu-item-538" class="blog-id-4 mlp-language-nav-item menu-item menu-item-type-language menu-item-object-mlp_language menu-item-538">
            <a rel="alternate" href="http://mysite.local/fr/?noredirect=fr_FR">
                 French FR</a>
        </li>
        <li id="menu-item-542" class="blog-id-1 mlp-language-nav-item menu-item menu-item-type-language menu-item-object-mlp_language mlp-current-language-item menu-item-542">
            <a rel="alternate" href="http://mysite.local/">
                English (UK)</a>
        </li>
    </ul>
</li>
<li id="menu-item-541" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children YO menu-item-541">
    <a href="#">America</a>
    <ul class="sub-menu">
        <li id="menu-item-539" class="blog-id-5 mlp-language-nav-item menu-item menu-item-type-language menu-item-object-mlp_language menu-item-539">
            <a rel="alternate" href="http://mysite.local/cr/?noredirect=es_CO">
                 CR</a>
        </li>
    </ul>
</li>
<li id="menu-item-540" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children YO menu-item-540">
    <a href="http://Asia">Asia</a>
    <ul class="sub-menu">
        <li id="menu-item-543" class="blog-id-6 mlp-language-nav-item menu-item menu-item-type-language menu-item-object-mlp_language menu-item-543">
            <a rel="alternate" href="http://cn.mysite.local/?noredirect=zh">
                 China</a>
        </li>
    </ul>
</li>

To

<select>
<option>EUROPE</option
<option>AMERICA</option
<option>ASIA</option
</select>

<select>
<option>French FR</option
<option>English (UK)</option
<option> CR</option
<option>China</option
</select>

Solution

  • as i understand you want to change all menu to select dropdown if i understand correctly you question then here is working function :

    class My_Walker_Nav_Menu_language  extends Walker_Nav_menu {
    
    
    public  function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ){ //li a span
    $url = '#' !== $item->url ? $item->url : '';
    
    if ($item->menu_item_parent == 0){ 
    $output .= '<select><option value="' . $url . '">' . $item->title;
    }
    $output .= ' <option value="' . $url . '">' . $item->title;
    
    }
    function end_el(&$output, $item, $depth = 0, $args = Array()){ // closing li a span
    if ($item->menu_item_parent == 0){ 
    $output .= "</option></select> "; // replace closing </li> with the option tag
    }
    $output .= "</option>"; // replace closing </li> with the option tag
    }
    
    }