<?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>风牛菜籽 &#187; 搜索</title>
	<atom:link href="http://www.caiyanpei.com/tag/%e6%90%9c%e7%b4%a2/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.caiyanpei.com</link>
	<description>拖延症重症患者</description>
	<lastBuildDate>Tue, 25 Mar 2025 01:51:19 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.6.1</generator>
		<item>
		<title>wordpress添加google自定义站内搜索功能</title>
		<link>http://www.caiyanpei.com/google-cse/</link>
		<comments>http://www.caiyanpei.com/google-cse/#comments</comments>
		<pubDate>Thu, 09 Dec 2010 06:34:06 +0000</pubDate>
		<dc:creator>tsai</dc:creator>
				<category><![CDATA[技术]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[搜索]]></category>

		<guid isPermaLink="false">http://cyp.me/?p=303</guid>
		<description><![CDATA[想在wordpress中添加google自定义搜索可能有两个原因：一是wordp &#8230; <a href="http://www.caiyanpei.com/google-cse/">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>想在wordpress中添加google自定义搜索可能有两个原因：一是wordpress本身搜索功能又慢又弱，二是有Adsense账号，这个搜索广告不用白不用，说不定还可以小弄点美刀什么的。<br />
对于添加google自定义搜索，有三种方法：<br />
1、把搜索结果放google页面上，这种方法通过点击站内google搜索框，然后跳转到google页面显示搜索结果，个人觉得这个方法比较不可取，因为把浏览者牵出站外了，对自己对浏览者都不是一个好的体验。本文对该方法不作描述。<br />
2、使用 Custom Search element让搜索结果显示在站内，这种方法针对没有Adsense账户的可以用下，该搜索结果显示在站内，展现结果跟第三种方法一样。<br />
3、把google自定义搜索框和搜索结果都放在wordpress主题中的文件，达到整过搜索过程在站内显示的目的。<br />
一、获取google自定义搜索框及搜索结果代码<br />
有Adsense账户的可以直接在Adsense中跟据向导生成搜索广告代码，在生成步骤中需要注意两点：<br />
1、指定的搜索网站列表要填写正确(如果只针对自己站点搜索，输入站点地址即可)，比如:http://www.xiuchezu.com/；<br />
2、选定搜索结果页时，可以先自己确定搜索结果页面（目前还没添加，将在下面说明），比如：http://www.xiuchezu.com/site-search，这是之后要添加生成的搜索结果代码的页面;<br />
然后所有设置步骤完成后，会生成搜索框和搜索结果代码。<br />
没有Adsense账户的只能使用 Custom Search element了：<br />
1、进入www.google.com.hk/cse/，当然这个google账户必须得有（请原谅我话多）。</p>
<p><img class="size-full wp-image-306 alignnone" title="image001" alt="" src="http://www.xiuchezu.com/wp-content/uploads/2010/12/image001.jpg" width="187" height="56" /></p>
<p>2、根据向导填写每个步骤的信息，要注意的是自定义搜索引擎免费版本有广告的，可以在添加后通过css处理iframe隐藏广告。<br />
3、搜索结果的样式可以选择几个默认样式或自定义样式。下一步得到最终的搜索结果代码。</p>
<p><img class="alignnone size-full wp-image-307" title="image002" alt="" src="http://www.xiuchezu.com/wp-content/uploads/2010/12/image002.jpg" width="562" height="298" /></p>
<p>二、将生成的搜索框、搜索结果代码放到wordpress主题中。<br />
在Adsense账户中获取的有两段代码，一是搜索框代码，另一是搜索结果代码：<br />
1、把获取的搜索框代码替换掉主题中searchform.php文件中的form节点的所有内容，如下:</p>
<pre class="brush: xml; title: ; notranslate">&lt;form id=&quot;cse-search-box&quot; action=&quot;http://www.xiuchezu.com/site-search&quot;&gt;
&lt;div&gt;&lt;input type=&quot;hidden&quot; name=&quot;cx&quot; value=&quot;********************&quot; /&gt; &lt;input type=&quot;hidden&quot; name=&quot;cof&quot; value=&quot;FORID:11&quot; /&gt; &lt;input type=&quot;hidden&quot; name=&quot;ie&quot; value=&quot;UTF-8&quot; /&gt; &lt;input type=&quot;text&quot; name=&quot;q&quot; size=&quot;31&quot; /&gt; &lt;input type=&quot;submit&quot; name=&quot;sa&quot; value=&quot;搜索&quot; /&gt;&lt;/div&gt;
&lt;/form&gt;</pre>
<pre class="brush: xml; title: ; notranslate">&lt;script type=&quot;text/javascript&quot; src=&quot;http://www.google.com.hk/cse/brand?form=cse-search-box&amp;amp;lang=zh-Hans&quot;&gt;// &lt;![CDATA[
mce:0
// ]]&gt;&lt;/script&gt;</pre>
<p>其中name=”cof”的input的 value=”FORID:11″查看google的解释，我之前设置forid值为10的时候，宽度是设置不了795以下的，改成11可以将宽度设置为600以下以适应主题。效果见本站点的搜索结果。<br />
name=”ie”的input是输入字符的编码，取决于在生成代码过程中国家区域的选择，我之前是GB2312，输入的中文就显示乱码，改成UTF-8就OK。<br />
修改搜索结果宽度则在搜索结果代码中修改，如下：</p>
<pre class="brush: jscript; title: ; notranslate"> var googleSearchFrameWidth = 630;</pre>
<p>2、剩下的就是要创建搜索结果页面，处理搜索结果代码了。<br />
新建一个文件如google-site-search.php，将要显示的博客站点框架加进来，比如head,sidebar,footer，再把搜索结果代码放到适当的地方，比如content中。<br />
接着就要小用下wordpress的自定义页面的功能了，将如下代码添加到google-site-search.php文件的最前面。</p>
<pre class="brush: xml; title: ; notranslate">&lt;!--?php /* Template Name:google-site-search */ ?--&gt;</pre>
<p>在wordpress后台添加新页面，在模板下拉框中选择刚添加的google-site-search，修改固定链接，如:http://showyo.net/site-search/，这个链接就是刚才在上面生成代码的时候用到的搜索结果链接地址。</p>
<p><img class="alignnone size-full wp-image-308" title="image004" alt="" src="http://www.xiuchezu.com/wp-content/uploads/2010/12/image004.jpg" width="232" height="220" /></p>
<p>针对使用Custom Search element方法：<br />
生成的代码只有搜索结果的，从建立文件到代码放置到新建页面都跟上面提到的Adsense搜索结果代码一致，不一样的是这边没有搜索框代码，但是这个是比较容易解决的，在searchform.php作小修改，只要使用“get”方法，表单结构自定义，将结果正确提交到搜索结果页面即可。类似代码如下：</p>
<pre class="brush: xml; title: ; notranslate">&lt;form action=&quot;http://www.xiuchezu.com/site-search/&quot; method=&quot;get&quot;&gt;
&lt;div class=&quot;s input&quot;&gt;&lt;input id=&quot;q&quot; type=&quot;text&quot; name=&quot;q&quot; /&gt; &lt;input type=&quot;hidden&quot; name=&quot;ie&quot; value=&quot;UTF-8&quot; /&gt;&lt;/div&gt;
&lt;div class=&quot;s&quot;&gt;&lt;input class=&quot;icon&quot; id=&quot;searchsubmit&quot; type=&quot;submit&quot; value=&quot;搜 索&quot; /&gt;&lt;/div&gt;
&lt;/form&gt;</pre>
<p>然后处理下CSS，让它和你的站点水乳交融就OK乐。具体可以看下本站的google自定义搜索结果的效果哦。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.caiyanpei.com/google-cse/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
