<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Bolo的博客 &#187; Template</title>
	<atom:link href="http://blog.imbolo.com/tag/template/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.imbolo.com</link>
	<description>一个设计爱好者杂七杂八的博客</description>
	<lastBuildDate>Wed, 23 Nov 2011 16:19:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>提高WordPress搜索体验</title>
		<link>http://blog.imbolo.com/improve-search-experience/</link>
		<comments>http://blog.imbolo.com/improve-search-experience/#comments</comments>
		<pubDate>Mon, 22 Feb 2010 06:06:12 +0000</pubDate>
		<dc:creator>bolo</dc:creator>
				<category><![CDATA[Develop]]></category>
		<category><![CDATA[Template]]></category>

		<guid isPermaLink="false">http://blog.imbolo.com/?p=1700</guid>
		<description><![CDATA[随着站内信息的增多，站内搜索成为了每个网站必不可少的功能。为了提高搜索体验，很多人采用Google Custom Search Engine服务作为站内搜索，可是使用搜索引擎进行站内搜索存在页面收录问题，因此对于新站和是不实用的，因此使用WordPress提供的搜索功能是最好的选择。]]></description>
			<content:encoded><![CDATA[<p>本篇文章讲的是提高WordPress自带的搜索功能的体验，使用Google CSE的可以飘过。</p>
<p>随着站内信息的增多，站内搜索成为了每个网站必不可少的功能。为了提高搜索体验，很多人采用Google Custom Search Engine服务作为站内搜索，可是使用搜索引擎进行站内搜索存在页面收录问题，因此对于新站和是不实用的，因此使用WordPress提供的搜索功能是最好的选择。相对于Google CSE，WordPress的搜索功能有两个明显不足：1.不能显示被检索到的条目数量；2.关键词没有高亮。我们可以通过修改模板来添加这两个功能。<span id="more-1700"></span></p>
<h3>显示检索到的条目数量</h3>
<p>打开主题的search.php（不同的主题可能有所不同），把下面的代码添加到适当的地方。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span> ?php
<span style="color: #666666; font-style: italic;">/* Search Count */</span>
	<span style="color: #000088;">$allsearch</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">&amp;new</span> WP_Query<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;s=<span style="color: #006699; font-weight: bold;">$s</span>&amp;showposts=-1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$key</span> <span style="color: #339933;">=</span> wp_specialchars<span style="color: #009900;">&#40;</span><span style="color: #000088;">$s</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$count</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$allsearch</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post_count</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;span class=&quot;search-terms&quot;&gt;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$key</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;/span&gt; &amp;mdash; '</span><span style="color: #339933;">.</span><span style="color: #000088;">$count</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' articles'</span><span style="color: #339933;">;</span>
	wp_reset_query<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></pre></td></tr></table></div>

<p>这样例如在搜索“bolo”时，检索到10篇文章，在将会提示：bolo — 0 articles。</p>
<h3>高亮关键词</h3>
<p>在得到搜索结果以后，我们通常需要浏览一下条目的摘要，以选择一条自己觉得最符合要求的进行点击。高亮关键词，能是人们更快地找到自己所需要的条目。要增加这个功能，同样修改search.php。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//查找&lt; ?php the_title(); ?&gt;，替换成&lt; ?php echo $title; ?&gt;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//在&lt; ?php echo $title; ?&gt;前增加以下代码：</span>
&nbsp;
<span style="color: #339933;">&lt;</span> ?php
	<span style="color: #000088;">$title</span> <span style="color: #339933;">=</span> get_the_title<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$keys</span><span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; &quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$s</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$title</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/('</span><span style="color: #339933;">.</span><span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'|'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$keys</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">')/iu'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&lt;strong class=&quot;search-excerpt&quot;&gt;\0'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$title</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>这个代码可以加粗文章标题里的关键词，另外你也可以通过增加样式使关键词更加突出。</p>
<p>本文代码来自<a href="http://www.wpbeginner.com" rel="external">http://www.wpbeginner.com</a></p>
<p  class="related_post_title">Random Posts</p><ul class="related_post"><li><a href="http://blog.imbolo.com/nosay-in/" title="不要说话，闭眼静听">不要说话，闭眼静听</a></li><li><a href="http://blog.imbolo.com/some-points-of-graphics-design/" title="图形设计应该注意的几个要点">图形设计应该注意的几个要点</a></li><li><a href="http://blog.imbolo.com/get-newest-taotao-updata/" title="在网页上调用最新滔滔">在网页上调用最新滔滔</a></li><li><a href="http://blog.imbolo.com/docin/" title="上豆丁，告别Google Doc">上豆丁，告别Google Doc</a></li><li><a href="http://blog.imbolo.com/100-best-fonts/" title="全球100个最佳字体">全球100个最佳字体</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://blog.imbolo.com/improve-search-experience/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
	</channel>
</rss>

