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
bass03 you have 2 mistakes:
- Menu-name should be menu location
- You need an “else { return $items;}” to return other menus without search.
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.
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;
}
}
Thank you. Worked like a charm!