Trouble adding search box to top menu
-
I am trying to add a search box to my top bar menu.
The problem I’m having is that the search box is appearing on both the top bar menu and the main menu. I only want it to appear on the top menu.
I used the following code in the functions.php file (please see note below about the section of the code I have put in bold):
add_filter('wp_nav_menu_items','add_search_box', 10, 2); function add_search_box($items, $args) { ob_start(); get_search_form(); $searchform = ob_get_contents(); ob_end_clean(); <strong>if( $args->theme_location == ‘nav-top’ )</strong> $items .= ' <li>' . $searchform . '</li> '; return $items; }
Here is a pastebin link if that doesn’t render properly here: https://pastebin.com/2QEWQc2k
The section in bold is where I’m supposed to be able to name the menu where I want the search box to appear. Whether I used frontier-menu-top, frontier-menu-primary, top-menu, or main-menu, the search box persists in appearing in both areas. If I use something arbitrary, it takes the search box off of both.
Is there any way to include it in one and exclude it from the other? As I mentioned, I only want it on the top menu.
- The topic ‘Trouble adding search box to top menu’ is closed to new replies.