WordPress菜单栏添加搜索框

WordPress主题在菜单栏尾部添加一个和菜单栏选项对齐的搜索按钮,原来我的办法是增加一个div,然后用css控制样式来实现。但是这样就会出现搜索按钮和菜单栏在某些情况下不一致的现象,万能的google上找到了解决办法,只需在functions.php中添加如下代码:

add_filter(‘wp_nav_menu_items’, ‘add_search_form_to_menu’, 10, 2);
function add_search_form_to_menu($items, $args) {
// If this isn’t the main navbar menu, do nothing
if( !($args->theme_location == ‘primary’) ) //搜索按钮显示的位置,pirmary或者secondary
return $items;
//搜索按钮样式
return $items . ‘<li class=”nav-search hidden-xs”>’ . ‘<a role=”button” data-toggle=”collapse” class=”btn btn-default” aria-expanded=”false” href=”#search”>
//此处搜索图标
</a>’ . ‘</li>’;
}

ok!大功告成。只需调整css即可,具体效果看本站菜单栏后的搜索按钮。

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注