欢迎光临
我们一直在努力

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网站使用的谷歌字体下载到自己的服务器

深圳SEO优化公司北京搜索引擎网站优化养殖行业网站优化运营木材行业网站优化方案永城网站关键词优化怎么收费无锡优化网站业务廊坊实力强的网站优化与推广安顺郴州企业网站优化方案惠州网站首页关键词优化费用云浮优化网站多少钱乌鲁木齐网站制作及优化崇安区seo网站优化去哪里找珠宝行业网站优化河南网站优化公司比较专业长沙网站优化全包专业网站关键词优化什么是网站优化外链网站优化免费测试南充网站优化服务有哪些东昌区网站seo优化排名松江区谷歌网站优化平台西安网站自己优化城厢区网站seo优化排名深圳南山区网站seo优化排名店优化网站三门峡郑州网站搜索优化优化一套公司网站需要多久安义县网站推广seo优化许昌企业网站优化外包陆丰网站优化公司南平网站优化多少钱歼20紧急升空逼退外机英媒称团队夜以继日筹划王妃复出草木蔓发 春山在望成都发生巨响 当地回应60岁老人炒菠菜未焯水致肾病恶化男子涉嫌走私被判11年却一天牢没坐劳斯莱斯右转逼停直行车网传落水者说“没让你救”系谣言广东通报13岁男孩性侵女童不予立案贵州小伙回应在美国卖三蹦子火了淀粉肠小王子日销售额涨超10倍有个姐真把千机伞做出来了近3万元金手镯仅含足金十克呼北高速交通事故已致14人死亡杨洋拄拐现身医院国产伟哥去年销售近13亿男子给前妻转账 现任妻子起诉要回新基金只募集到26元还是员工自购男孩疑遭霸凌 家长讨说法被踢出群充个话费竟沦为间接洗钱工具新的一天从800个哈欠开始单亲妈妈陷入热恋 14岁儿子报警#春分立蛋大挑战#中国投资客涌入日本东京买房两大学生合买彩票中奖一人不认账新加坡主帅:唯一目标击败中国队月嫂回应掌掴婴儿是在赶虫子19岁小伙救下5人后溺亡 多方发声清明节放假3天调休1天张家界的山上“长”满了韩国人?开封王婆为何火了主播靠辱骂母亲走红被批捕封号代拍被何赛飞拿着魔杖追着打阿根廷将发行1万与2万面值的纸币库克现身上海为江西彩礼“减负”的“试婚人”因自嘲式简历走红的教授更新简介殡仪馆花卉高于市场价3倍还重复用网友称在豆瓣酱里吃出老鼠头315晚会后胖东来又人满为患了网友建议重庆地铁不准乘客携带菜筐特朗普谈“凯特王妃P图照”罗斯否认插足凯特王妃婚姻青海通报栏杆断裂小学生跌落住进ICU恒大被罚41.75亿到底怎么缴湖南一县政协主席疑涉刑案被控制茶百道就改标签日期致歉王树国3次鞠躬告别西交大师生张立群任西安交通大学校长杨倩无缘巴黎奥运

深圳SEO优化公司 XML地图 TXT地图 虚拟主机 SEO 网站制作 网站优化