Okay
  Public Ticket #855759
Search box in main menu
Closed

Comments

  •  3
    Mireia started the conversation

    Hello,

    I'm trying to add a searchbox in header main navigation menu. I found this code and it worked it

    add_filter( 'wp_nav_menu_items','add_search_box', 10, 2 );
    function add_search_box( $items, $args ) {
            $items .= '<li>' . get_search_form( false ) . '</li>';
        return $items;
    }
    The problem is that it adds the searchbox in all menus of the site. So I tried to add a filter to show it just in the 'main' menu (tha's the name of the menu):
    add_filter( 'wp_nav_menu_items','add_search_box', 10, 2 );
    function add_search_box( $items, $args ) {
        if ( 'main' == $args->theme_location )
            $items .= '<li>' . get_search_form( false ) . '</li>';
        return $items;
    }
    As it didn't work, I change 'main' for 'primary', but I don't know why it still does not work. Could you tell me how to filter the function to the main menu?

    Thanks,

    Best,

    Mire

  • [deleted] replied

    Hi Mire,

    Instead of main and primary set main-menu and that is the correct location of our Main Menu in the header.

    Your second menu filter function will work with this and it should look like:

    add_filter( 'wp_nav_menu_items','add_search_box', 10, 2 );
    
    function add_search_box( $items, $args ) {
        if ( 'main-menu' == $args->theme_location )
            $items .= '<li>' . get_search_form( false ) . '</li>';
        return $items;
    }
  •  3
    Mireia replied

    Hi Arlind, I'm going to check it. Thanks!

  •  3
    Mireia replied

    It wordked perfectly. Thanks!

  • [deleted] replied

    Great to hear that!