简体   繁体   English

单击/切换显示搜索字段时,在导航菜单中显示搜索图标-Wordpress

[英]show search icon in nav menu, when clicked/toggle show search field - Wordpress

I'm trying to add a search icon to my navbar on wordpress using wpresidence theme on Wordpress, 我正在尝试使用Wordpress上的wpresidence主题将搜索图标添加到wordpress上的导航栏中,

Once clicked, i would like the search bar to appear, i wouldn't mind if it makes use of the current advanced search feature. 单击后,我希望出现搜索栏,我不介意它是否利用了当前的高级搜索功能。

Would be very grateful for your help 非常感谢您的帮助

function add_last_nav_item($items, $args) {
  if ($args->menu == 'header_menu') {
        $homelink = get_search_form();
        $items = $items;
        $items .= '<li>'.$homelink.'</li>';
        return $items;
  }
  return $items;
}
add_filter( 'wp_nav_menu_items', 'add_last_nav_item', 10, 2 );

--- i've tried using bobsearch plugin and it didn't work and also entered this into my functions.php code but it didnt work ---我尝试使用bobsearch插件,但它不起作用,也将其输入到我的functions.php代码中,但它没有作用

Your hook looks fine. 你的钩子看起来不错。 But the condition maybe wrong. 但是条件可能不对。 If you want to check theme location when register with them, try this: 如果要在注册时检查主题位置,请尝试以下操作:

if($args->theme_location == "header_menu")

$args objects is an object containing wp_nav_menu() arguments. $ args对象是包含wp_nav_menu()参数的对象。

https://developer.wordpress.org/reference/functions/wp_nav_menu/ https://developer.wordpress.org/reference/functions/wp_nav_menu/

Here is searchform.php 这是searchform.php

<form role="search" method="get" class="search-form" action="<?php echo home_url(); ?>">
    <label>
        <input class="search-field" placeholder="Search" value="" name="s" type="search">
    </label>
    <input class="search-submit" value="Search" type="submit">
</form>

Demo here: https://jsfiddle.net/zdcmmfe9/1/ 演示在这里: https : //jsfiddle.net/zdcmmfe9/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM