0

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("&nbsp;", $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

enter image description here

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">&nbsp;&nbsp;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">&nbsp;&nbsp;Contact Us</option>
            <option id="menu-item-63" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-63">&nbsp;&nbsp;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.

2
  • The walker menu doesn't seem to be working. I'm checking to find problem...
    – tamilsweet
    Commented Jun 16, 2012 at 11:57
  • @tamilsweet I tried the crawler with the twenyeleven theme and it worked
    – ilyo
    Commented Jun 16, 2012 at 13:52

1 Answer 1

1

Maybe you are doing it wrong? Here's a pointer...

In functions.php

// This theme uses wp_nav_menu() in one location.
register_nav_menus( array(
    'primary' => 'Primary Menu',
) );

In header.php (or elsewhere)

<?php wp_nav_menu( array( 'theme_location' => 'primary' ) ); ?>
4
  • what you offered works, which means there is a problem with the custom crawler.
    – ilyo
    Commented Jun 16, 2012 at 15:29
  • @IlyaD so...? what exactly is your problem now?
    – its_me
    Commented Jun 16, 2012 at 16:15
  • the crawler function gives some weird output
    – ilyo
    Commented Jun 16, 2012 at 16:29
  • @IlyaD Well, I don't know too much PHP, but why use "the crawler function" when you have something that works? What I am showing you is how the core WordPress developers do it. Anyway, I leave it for you to decide. But please don't keep editing your questions with new questions. It's tiring. :)
    – its_me
    Commented Jun 16, 2012 at 16:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.