I want to make a custom menu that will be a select-box,
I registered a custom menu:
add_action( 'init', 'register_my_menu' );
function register_my_menu() {
register_nav_menu( 'primary', 'Primary Menu' );
}
I added a custom menu crawler to the heading using this: How to use wp_nav_menu to create a select menu dropdown?
<nav class="page-name">
<?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(" ", $depth * 2).$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', $output);
}
function end_el(&$output, $item, $depth){
$output .= "</option>\n"; // replace closing </li> with the option tag
}
}
wp_nav_menu(array(
'theme_location' => 'primary', // your theme location here
'walker' => new Walker_Nav_Menu_Dropdown(),
'items_wrap' => '<select>%3$s</select>',
));
?>
</nav>
And created a custom menu in the WP-Admin
But still the menu doesn't show. What am I missing?
Update
Apparently The crawler function does make an output but it is messed up,
When I use just register_nav_menu( 'primary', 'Primary Menu' );
I get a working menu.
<nav class="page-name">
<div class="menu">
<ul>
<option id="menu-item-54" class="menu-item-54"><a></a></option>
<option id="menu-item-24" class="menu-item-24"><a></a></option>
<option id="menu-item-30" class="menu-item-30"><a></a></option>
<option id="menu-item-18" class="menu-item-18"><a></a></option>
<option id="menu-item-22" class="menu-item-22"><a></a></option>
<option id="menu-item-26" class="menu-item-26"><a></a></option>
<option id="menu-item-13" class="menu-item-13"><a></a></option>
<option id="menu-item-28" class="menu-item-28"><a></a></option>
<option id="menu-item-15" class="menu-item-15"><a></a></option>
<option id="menu-item-2" class="menu-item-2"><a></a></option>
<option id="menu-item-20" class="menu-item-20"><a></a></option>
</ul>
</div>
</nav>
This is what I get when using the exact same function in twenteleven theme (which is what I want):
<nav class="page-name">
<div class="menu-nav-menu-container">
<select>
<option id="menu-item-59" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-59">Sample Page </option>
<option id="menu-item-60" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-60"> News</option>
<option id="menu-item-65" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-65">About </option>
<option id="menu-item-62" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-62"> Contact Us</option>
<option id="menu-item-63" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-63"> Our Location</option>
<option id="menu-item-61" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-61">History</option>
<option id="menu-item-64" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-64">Links</option>
</select>
</div>
</nav>
Update
I solved the problem. apparently the custom menu was selected when I used another theme and I assumed every theme will uset. well - no! I had to select custom menu for each theme separately.