首页» 教程» Wordpress教程» WordPress 搜索下拉提示-进阶教程(二十一)

WordPress 搜索下拉提示-进阶教程(二十一)

Hello,嗨,大家好,我是哈喽猿。

这里是哈喽猿网

今天推送的是wordpress教程的文章,感谢您宝贵的时间阅读

WordPress进阶教程目录:

1.WordPress禁止F12和内容复制

2.WordPress GZIP压缩提高网站访问速度   

3.WordPress 自动缩略图   

4.WordPress 移除版权版本信息   

5.WordPress 数据库清理优化WP-Optimize   

6.WordPress 发送邮件功能(不用插件)   

7.WordPress 网站加速神器   

8.WordPress 文章自动换行   

9.WordPress 自动截取限定数字的摘要   

10.WordPress 大前端主题 实时推送百度熊掌号

11.WordPress 图片自动加水印

12.WordPress 图片加水印   

13.WordPress 搜索相关性和精准度   

14.WordPress 站点统计功能代码   

15.WordPress 站点统计功能代码   

16.WordPress 导入导出工具   

17.WordPress 文章导入   

18.WordPress 文章刷新   

19.WordPress 文章Tag标签内链优化   

20.WordPress 内容被复制后添加本站文章链接

21.WordPress 搜索下拉提示   

22.WordPress 敏感词汇屏蔽   

23.WordPress 添加相关文章功能   

24.WordPress 阿里云邮件   

25.WordPress 自动采集发布 长腿蜘蛛 WP-CTspider   

 

WordPress 搜索出现相关内容

当我们在百度搜索时候,只要你键入关键词,就会自动弹出一个下拉框,提示相关内容,所以炫目的功能一定要添加到我们的博客中!实现这种功能有两种方法。两种方法的效果略有不同,哈喽猿稍后将详细解释。

 

方法1:

插件@Vango制作了一个插件,可以实现上述功能。很简单,你不需要做任何事情,直接下载安装就行了(插件还没有提交给官方,不能在线安装):插件下载地址

 

其实这个插件有一个缺点:只能匹配标签,不能直接匹配文章内容,这使得插件感觉非常不切实际。

WordPress 搜索下拉提示

方法2:

代码比插件方法更实用,可以与文章匹配,但是对于没有任何技术的新手来说是一个挑战

 

①首先打开search.php,找到下面代码这一行:

get_header<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>

替换成:

<span style="color: #990000;">array_push</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array_posts</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;title&quot;</span><span style="color: #339933;">=&gt;</span>get_the_title<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;url&quot;</span><span style="color: #339933;">=&gt;</span>get_permalink<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>

②找到

get_footer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>

替换为:

get_footer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>

③接下来对搜索框的代码优化,参考下面的代码:

<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;search-container&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;ajax_search&quot;</span>&gt;</span>

④在 footer.php 中的,找到下面代码

<span style="color: #000000; font-weight: bold;">&lt;?php</span> wp_footer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>

在上面代码之前加入以下代码

<span style="color: #339933;">&lt;</span>script<span style="color: #339933;">&gt;</span>var home_url<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&lt;?php echo esc_url(home_url('/')); ?&gt;&quot;</span><span style="color: #339933;">;&lt;/</span>script<span style="color: #339933;">&gt;</span>

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

<span style="color: #666666; font-style: italic;">//search</span>
var input_search <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;#search-input&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
function makeAjaxSearch<span style="color: #009900;">&#40;</span>result<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>result.<span style="color: #006633;">length</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;#search_filtered&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&lt;li&gt;&lt;a href=&quot;javascript:vold(0)&quot;&gt;&lt;strong&gt;这能搜到嘛?&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;#search_filtered&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>var i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> result.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> $<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;#search_filtered&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&lt;li&gt;&lt;a href=&quot;'</span> <span style="color: #339933;">+</span> result<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;url&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">'&quot;&gt;'</span> <span style="color: #339933;">+</span> result<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;title&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">'&lt;/a&gt;&lt;/li&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
var delaySearch<span style="color: #339933;">;</span>
&nbsp;
function startSearch<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	$.<span style="color: #006633;">ajax</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
		type<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;GET&quot;</span>,
		url<span style="color: #339933;">:</span> home_url, 
		data<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;s=&quot;</span> <span style="color: #339933;">+</span> input_search.<span style="color: #006633;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>,
		dataType<span style="color: #339933;">:</span> <span style="color: #0000ff;">'json'</span>,
		success<span style="color: #339933;">:</span> function <span style="color: #009900;">&#40;</span>result<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			makeAjaxSearch<span style="color: #009900;">&#40;</span>result<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			console.<span style="color: #006633;">log</span><span style="color: #009900;">&#40;</span>result<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
var event_ajax_search <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
	bind_event<span style="color: #339933;">:</span> function <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		input_search.<span style="color: #006633;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'keyup'</span>, function <span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>input_search.<span style="color: #006633;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">&quot;&quot;</span> <span style="color: #339933;">&amp;&amp;</span> e.<span style="color: #006633;">keyCode</span> <span style="color: #339933;">!=</span> <span style="color: #cc66cc;">40</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>delaySearch<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					clearTimeout<span style="color: #009900;">&#40;</span>delaySearch<span style="color: #009900;">&#41;</span>
				<span style="color: #009900;">&#125;</span>
				delaySearch <span style="color: #339933;">=</span> setTimeout<span style="color: #009900;">&#40;</span>startSearch, <span style="color: #cc66cc;">200</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>e.<span style="color: #006633;">keyCode</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">40</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				search_filtered.<span style="color: #006633;">moveable</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#125;</span>,
	unbind_event<span style="color: #339933;">:</span> function <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		input_search.<span style="color: #006633;">unbind</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'keyup'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
var search_filtered <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
	moveable<span style="color: #339933;">:</span> function <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		var current <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'#search_filtered'</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">find</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'a'</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">eq</span><span style="color: #009900;">&#40;</span>current<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">focus</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;keydown.search_result&quot;</span>, function <span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>e.<span style="color: #006633;">keyCode</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">40</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
				<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>current <span style="color: #339933;">&gt;=</span> $<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'#search_filtered'</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">find</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'a'</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					current <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
&nbsp;
				$<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'#search_filtered'</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">find</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'a'</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">eq</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">++</span>current<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">focus</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				e.<span style="color: #006633;">preventDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #009900;">&#125;</span>
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>e.<span style="color: #006633;">keyCode</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">38</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>current <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					current <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'#search_filtered'</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">find</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'a'</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
&nbsp;
				$<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'#search_filtered'</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">find</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'a'</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">eq</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">--</span>current<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">focus</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				e.<span style="color: #006633;">preventDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>,
	hide<span style="color: #339933;">:</span> function <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">unbind</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;keyup.search_result&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'#search_filtered'</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">fadeOut</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
input_search.<span style="color: #006633;">focus</span><span style="color: #009900;">&#40;</span>function <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	event_ajax_search.<span style="color: #006633;">bind_event</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">blur</span><span style="color: #009900;">&#40;</span>function <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	event_ajax_search.<span style="color: #006633;">unbind_event</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>

 

⑥参考CSS

<span style="color: #cc00cc;">#search-input</span><span style="color: #00AA00;">&#123;</span><span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">left</span><span style="color: #00AA00;">;</span><span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span><span style="color: #993333;">none</span><span style="color: #00AA00;">;</span><span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span><span style="color: #933;">22px</span><span style="color: #00AA00;">;</span><span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;">150px</span><span style="color: #00AA00;">;</span><span style="color: #000000; font-weight: bold;">padding-right</span><span style="color: #00AA00;">:</span><span style="color: #933;">25px</span><span style="color: #00AA00;">;</span><span style="color: #000000; font-weight: bold;">line-height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">22px</span><span style="color: #00AA00;">;</span><span style="color: #000000; font-weight: bold;">text-indent</span><span style="color: #00AA00;">:</span> <span style="color: #933;">10px</span><span style="color: #00AA00;">;</span><span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span><span style="color: #933;">12px</span><span style="color: #00AA00;">;</span><span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #dc143c;">transparent</span><span style="color: #00AA00;">;</span><span style="color: #000000; font-weight: bold;">background-image</span><span style="color: #00AA00;">:</span><span style="color: #9932cc;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000; font-style: italic;">img/search.png</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span><span style="color: #000000; font-weight: bold;">background-repeat</span><span style="color: #00AA00;">:</span><span style="color: #993333;">no-repeat</span><span style="color: #00AA00;">;</span><span style="color: #000000; font-weight: bold;">background-position</span><span style="color: #00AA00;">:</span><span style="color: #993333;">right</span> <span style="color: #993333;">center</span><span style="color: #00AA00;">&#125;</span>

 

发表评论

0 评论

提供最优质的资源集合

站长留言