WordPress.org

Ready to get started?Download WordPress

Forums

Search box on Navigation Menu
Search box for specific Navigation Menu (6 posts)

  1. Cheatsrichter
    Member
    Posted 1 year ago #

    Is it possible to select a specific menu for the search box. I just need it for the main primary menu but there are two other menus that are getting a search box.

    Hope there is a quick solution for that.

    Thanks and kind regards

    http://wordpress.org/extend/plugins/search-box-on-navigation-menu/

  2. bass03
    Member
    Posted 7 months ago #

    Hello
    I found a similar code, but I can not solve the problem. This is the code:

    add_filter('wp_nav_menu_items', 'add_search_form', 10, 2);
    function add_search_form($items, $args) {
    if( $args->theme_location == 'MENU-NAME' )
            $items .= '<li class="search"><form role="search" method="get" id="searchform" action="'.home_url( '/' ).'"><input type="text" value="search" name="s" id="s" /><input type="submit" id="searchsubmit" value="'. esc_attr__('Search') .'" /></form></li>';
            return $items;
    }

    If you find the solution, please contact me. Thank you!
    regards

  3. Mike
    Member
    Posted 5 months ago #

    bass03 you have 2 mistakes:

    1. Menu-name should be menu location
    2. You need an "else { return $items;}" to return other menus without search.
  4. shedthegear
    Member
    Posted 4 months ago #

    Hi,

    I am trying to figure this out as well.

    Mike, I'm not sure what you mean?

    How should the code look if I only want the search box to show up in my menu named "top menu"? what would 'menu location' be?

    I'm not a coder so where would the "else { return $items;}" code go?

    Thanks.

  5. Mike
    Member
    Posted 4 months ago #

    Here is what I scraped together which works:
    `
    add_filter('wp_nav_menu_items','add_search_box', 10, 2);
    function add_search_box($items, $args) {
    if( $args->theme_location == 'primary' )
    {
    ob_start();
    get_search_form();
    $searchform = ob_get_contents();
    ob_end_clean();
    $items .= $searchform ;
    return $items;
    }
    else
    {
    return $items;
    }
    }

  6. shedthegear
    Member
    Posted 4 months ago #

    Thank you. Worked like a charm!

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.