I am creating a menu manually and trying to include the search form.

this is taken from the Wordpress Docs "Building a simple menu list" at http://codex.wordpress.org/Function_Reference/wp_get_nav_menu_items.

I have added an additional <li> element before completing the menu and added the search form.

            // Get the nav menu based on $menu_name (same as 'theme_location' or 'menu' arg to wp_nav_menu)
            // This code based on wp_nav_menu's code to get Menu ID from menu slug

            $menu_name = 'main-menu';

            if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $menu_name ] ) ) {

                $menu = wp_get_nav_menu_object( $locations[ $menu_name ] );

                $menu_items = wp_get_nav_menu_items($menu->term_id);

                $menu_list = '<ul id="menu-' . $menu_name . '">';

                foreach ( (array) $menu_items as $key => $menu_item ) {
                    $title = $menu_item->title;
                    $url = $menu_item->url;
                    $menu_list .= '<li><a href="' . $url . '">' . $title . '</a></li>';
                $menu_list .= '<li>' . get_search_form(false) . '</li>';
                $menu_list .= '</ul>';
            } else {
                $menu_list = '<ul><li>No Menu</li></ul>';
            echo $menu_list;

The problem is in the output.

<div class="mainnav" role="navigation">

<form role="search" method="get" id="searchform" action="/">
  <input type="text" value="" name="s" id="s" size="40" placeholder="Search" alt="Site Search">
  <input type="submit" id="searchsubmit" value="Search">
<ul id="menu-main-menu">
  <li><a href="#">Home</a></li>
  <li><a href="#">Sample Page</a></li>


Could someone tell me why the search is printing before the menu even though I specify echo = false?

1 Answer 1


this might be the cause:

The $echo parameter is ignored when searchform.php is present. There is a issue in the WordPress Trac concerning this.

from the codex: http://codex.wordpress.org/Function_Reference/get_search_form#Notes


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.