欢迎光临
我们一直在努力

WordPress 搜索下拉关键词提示

建站超值云服务器,限时71元/月

本文目录
[隐藏]

  • 1方法一、插件
  • 2方法二、代码
  • 3总结

我想了半天,也没给这篇文章一个准确的标题,来张图说明大家肯定就明白了:

WordPress 搜索下拉关键词提示

百度搜索,只要你打上关键词,就会自动弹出下拉框提示有关内容,这么炫的功能我们一定要给自己的博客加上!

方法有两种,两种方法效果略有不同,稍后会详细解释。

方法一、插件

@万戈 制作了一个插件,可以实现上述功能,非常简单,什么都不用做,直接下载安装即可(该插件未被提交到官方,无法在线安装):官方下载 | 备用下载

其实这个插件有一个缺点:只能匹配标签,不能直接匹配文章内容,这让插件感觉很不实用。

WordPress 搜索下拉关键词提示

方法二、代码

代码比插件法更实用,可以匹配出文章,但对没有什么技术的新手,实现却是一个挑战(该代码来自 @大发)。

1、首先打开主题的 search.php,找到:

1
get_header();

get_header();

替换成:

1
2
3
4
5
6
7
8
9
10
if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){
		$array_posts = array ();
		if (have_posts()) :
			 while (have_posts()) : the_post();
				 array_push($array_posts, array("title"=>get_the_title(),"url"=>get_permalink()));
			 endwhile;
		endif;
		echo json_encode($array_posts);
	} else {
get_header();

if(isset($_SERVER[‘HTTP_X_REQUESTED_WITH’]) && strtolower($_SERVER[‘HTTP_X_REQUESTED_WITH’]) == ‘xmlhttprequest’){ $array_posts = array (); if (have_posts()) : while (have_posts()) : the_post(); array_push($array_posts, array(“title”=>get_the_title(),”url”=>get_permalink())); endwhile; endif; echo json_encode($array_posts); } else { get_header();

再找到:

1
get_footer();

get_footer();

替换为:

1
get_footer();}

get_footer();}

然后对搜索框代码进行改造,给搜索结果做定位。按照下边的例子修改搜索框:

1
2
3
4
5
6
<div id="search-container" class="ajax_search">
	<form method="get" id="searchform" action="<?php echo esc_url(home_url('/')); ?>">
		<div class="filter_container"><input type="text" value="" autocomplete="off" placeholder="输入内容并回车" name="s" id="search-input"/><ul id="search_filtered" class="search_filtered"></ul> </div>
		<input type="submit" name="submit" id="searchsubmit" class="searchsubmit" value=""/>
	</form>
</div>

<div id=”search-container” class=”ajax_search”> <form method=”get” id=”searchform” action=”<?php echo esc_url(home_url(‘/’)); ?>”> <div class=”filter_container”><input type=”text” value=”” autocomplete=”off” placeholder=”输入内容并回车” name=”s” id=”search-input”/><ul id=”search_filtered” class=”search_filtered”></ul> </div> <input type=”submit” name=”submit” id=”searchsubmit” class=”searchsubmit” value=””/> </form> </div>

接着在 footer.php 中的:

1
<?php wp_footer(); ?>

<?php wp_footer(); ?>

前加入下边的代码:

1
<script>var home_url="<?php echo esc_url(home_url('/')); ?>";</script>

<script>var home_url=”<?php echo esc_url(home_url(‘/’)); ?>”;</script>

最后在 JS 文件中贴上下边的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//search
var input_search = $("#search-input");
function makeAjaxSearch(result) {
	if (result.length == 0) {
		$("#search_filtered").empty().show().append('<li><a href="javascript:vold(0)"><strong>这能搜到嘛?</strong></a></li>');
	} else {
		$("#search_filtered").empty().show();
		for (var i = 0; i < result.length; i++) $("#search_filtered").append('<li><a href="'%20+ result[i]["url"] + '">'%20+ result[i]["title"] + '</a></li>');
	}
}
var delaySearch;
 
function startSearch() {
	$.ajax({
		type: "GET",
		url: home_url, 
		data: "s=" + input_search.val(),
		dataType: 'json',
		success: function (result) {
			makeAjaxSearch(result);
			console.log(result);
		}
	});
}
var event_ajax_search = {
	bind_event: function () {
		input_search.bind('keyup', function (e) {
			if (input_search.val() != "" && e.keyCode != 40) {
				if (delaySearch) {
					clearTimeout(delaySearch)
				}
				delaySearch = setTimeout(startSearch, 200);
			}
			if (e.keyCode == 40) {
				search_filtered.moveable();
			}
		})
	},
	unbind_event: function () {
		input_search.unbind('keyup');
	}
};
var search_filtered = {
	moveable: function () {
		var current = 0;
		$('#search_filtered').find('a').eq(current).focus();
		$(document).bind("keydown.search_result", function (e) {
			if (e.keyCode == 40) {
 
				if (current >= $('#search_filtered').find('a').size()) {
					current = 0;
				}
 
				$('#search_filtered').find('a').eq(++current).focus();
				e.preventDefault();
 
			}
			if (e.keyCode == 38) {
				if (current < 0) {
					current = $('#search_filtered').find('a').size() - 1;
				}
 
				$('#search_filtered').find('a').eq(--current).focus();
				e.preventDefault();
			}
		});
	},
	hide: function () {
		$(document).unbind("keyup.search_result");
		$('#search_filtered').fadeOut();
	}
};
input_search.focus(function () {
	event_ajax_search.bind_event();
}).blur(function () {
	event_ajax_search.unbind_event();
});

//search var input_search = $(“#search-input”); function makeAjaxSearch(result) { if (result.length == 0) { $(“#search_filtered”).empty().show().append(‘<li><a href=”javascript:vold(0)”><strong>这能搜到嘛?</strong></a></li>’); } else { $(“#search_filtered”).empty().show(); for (var i = 0; i < result.length; i++) $(“#search_filtered”).append(‘<li><a href=”‘%20+%20result[i][“url”] + ‘”>’%20+%20result[i][“title”] + ‘</a></li>’); } } var delaySearch; function startSearch() { $.ajax({ type: “GET”, url: home_url, data: “s=” + input_search.val(), dataType: ‘json’, success: function (result) { makeAjaxSearch(result); console.log(result); } }); } var event_ajax_search = { bind_event: function () { input_search.bind(‘keyup’, function (e) { if (input_search.val() != “” && e.keyCode != 40) { if (delaySearch) { clearTimeout(delaySearch) } delaySearch = setTimeout(startSearch, 200); } if (e.keyCode == 40) { search_filtered.moveable(); } }) }, unbind_event: function () { input_search.unbind(‘keyup’); } }; var search_filtered = { moveable: function () { var current = 0; $(‘#search_filtered’).find(‘a’).eq(current).focus(); $(document).bind(“keydown.search_result”, function (e) { if (e.keyCode == 40) { if (current >= $(‘#search_filtered’).find(‘a’).size()) { current = 0; } $(‘#search_filtered’).find(‘a’).eq(++current).focus(); e.preventDefault(); } if (e.keyCode == 38) { if (current < 0) { current = $(‘#search_filtered’).find(‘a’).size() – 1; } $(‘#search_filtered’).find(‘a’).eq(–current).focus(); e.preventDefault(); } }); }, hide: function () { $(document).unbind(“keyup.search_result”); $(‘#search_filtered’).fadeOut(); } }; input_search.focus(function () { event_ajax_search.bind_event(); }).blur(function () { event_ajax_search.unbind_event(); });

参考 CSS:

1
2
3
4
5
6
7
.filter_container {display: inline-block;position: relative;}
.ajax_search .search_filtered a {display: block;font-size: 12px;overflow: hidden;padding: 7px 12px 7px 10px;text-overflow: ellipsis;white-space: nowrap;width: 153px;color: #D14836;}
.ajax_search .search_filtered {background-color: rgba(255, 255, 255, 0.95);left: 0;position: absolute;text-align: left;top: 102%;z-index: 200;}
#search-input{float: left;border:none;height:22px;width:150px;padding-right:25px;line-height: 22px;text-indent: 10px;font-size:12px;background-color: transparent;background-image:url(img/search.png);background-repeat:no-repeat;background-position:right center}
#search-input:focus{background-color: #fff;}
#searchsubmit{display: none;}
.ajax_search .search_filtered a:hover, .ajax_search .search_filtered a:focus {background-color: rgba(0, 0, 0, 0.03);text-decoration: none;outline:thin 

.filter_container {display: inline-block;position: relative;} .ajax_search .search_filtered a {display: block;font-size: 12px;overflow: hidden;padding: 7px 12px 7px 10px;text-overflow: ellipsis;white-space: nowrap;width: 153px;color: #D14836;} .ajax_search .search_filtered {background-color: rgba(255, 255, 255, 0.95);left: 0;position: absolute;text-align: left;top: 102%;z-index: 200;} #search-input{float: left;border:none;height:22px;width:150px;padding-right:25px;line-height: 22px;text-indent: 10px;font-size:12px;background-color: transparent;background-image:url(img/search.png);background-repeat:no-repeat;background-position:right center} #search-input:focus{background-color: #fff;} #searchsubmit{display: none;} .ajax_search .search_filtered a:hover, .ajax_search .search_filtered a:focus {background-color: rgba(0, 0, 0, 0.03);text-decoration: none;outline:thin dotted}

总结

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载: IDC资讯中心 » WordPress 搜索下拉关键词提示
分享到: 更多 ( 0)

相关推荐

  •       WordPress 站点集成 Google 自定义搜索引擎
  •       WordPress搜索框关键词提示插件 WP Search Auto Match
  •       让 WordPress 只搜索文章的标题
  •       WordPress 搜索结果中排除特定的页面、文章和分类
  •       WordPress搜索结果只有一篇文章时自动跳转到文章
  •       WordPress提高搜索结果的相关性(准确度)
  •       WordPress自定义文章作者名称
  •       将WordPress网站使用的谷歌字体下载到自己的服务器

玻璃钢生产厂家吴忠玻璃钢树池公司钦州玻璃钢茶几厂陕西玻璃钢前台厂南昌不锈钢家具厂家来宾玻璃钢花池定制云南玻璃钢花坛厂家直销湘潭玻璃钢加工梅州玻璃钢前台厂家德州不锈钢花盆制作徐州玻璃钢动物雕塑定做南阳玻璃钢浮雕生产厂家安徽玻璃钢装饰工程厂家直销扬州玻璃钢花钵价格梅州商场美陈制造内江玻璃钢花槽生产厂家广元玻璃钢浮雕厂家直销湖南玻璃钢装饰厂家直销梅州玻璃钢浮雕哪家好贵州玻璃钢坐凳生产厂家金华玻璃钢医疗外壳批发淮安玻璃钢种植池厂家丽江不锈钢家具多少钱三明玻璃钢设备外壳制造榆林玻璃钢设备外壳价格苏州商业美陈厂家湘潭玻璃钢机械外壳甘肃玻璃钢装饰加工济宁玻璃钢沙发生产厂家龙岩玻璃钢花盆定制鞍山不锈钢家具哪家好香港通过《维护国家安全条例》两大学生合买彩票中奖一人不认账让美丽中国“从细节出发”19岁小伙救下5人后溺亡 多方发声卫健委通报少年有偿捐血浆16次猝死汪小菲曝离婚始末何赛飞追着代拍打雅江山火三名扑火人员牺牲系谣言男子被猫抓伤后确诊“猫抓病”周杰伦一审败诉网易中国拥有亿元资产的家庭达13.3万户315晚会后胖东来又人满为患了高校汽车撞人致3死16伤 司机系学生张家界的山上“长”满了韩国人?张立群任西安交通大学校长手机成瘾是影响睡眠质量重要因素网友洛杉矶偶遇贾玲“重生之我在北大当嫡校长”单亲妈妈陷入热恋 14岁儿子报警倪萍分享减重40斤方法杨倩无缘巴黎奥运考生莫言也上北大硕士复试名单了许家印被限制高消费奥巴马现身唐宁街 黑色着装引猜测专访95后高颜值猪保姆男孩8年未见母亲被告知被遗忘七年后宇文玥被薅头发捞上岸郑州一火锅店爆改成麻辣烫店西双版纳热带植物园回应蜉蝣大爆发沉迷短剧的人就像掉进了杀猪盘当地回应沈阳致3死车祸车主疑毒驾开除党籍5年后 原水城县长再被查凯特王妃现身!外出购物视频曝光初中生遭15人围殴自卫刺伤3人判无罪事业单位女子向同事水杯投不明物质男子被流浪猫绊倒 投喂者赔24万外国人感慨凌晨的中国很安全路边卖淀粉肠阿姨主动出示声明书胖东来员工每周单休无小长假王树国卸任西安交大校长 师生送别小米汽车超级工厂正式揭幕黑马情侣提车了妈妈回应孩子在校撞护栏坠楼校方回应护栏损坏小学生课间坠楼房客欠租失踪 房东直发愁专家建议不必谈骨泥色变老人退休金被冒领16年 金额超20万西藏招商引资投资者子女可当地高考特朗普无法缴纳4.54亿美元罚金浙江一高校内汽车冲撞行人 多人受伤

玻璃钢生产厂家 XML地图 TXT地图 虚拟主机 SEO 网站制作 网站优化