本来呢我是想弄Google CSE自定义搜索引擎来代替网站自身的搜索服务的,那时候对WordPress很陌生,结果呢就不提了,大家知道的。最早知道Google Ajax搜索是从老肥的”打造完美的 ajax 版 Google 自定义搜索”,那时候看得我一头的雾水,但是直觉告诉我这个功能很炫,所以呢,就将其添加进了书签,它这一躺就将近二个月。在Z-Turn上线测试后,就突然想起它了,然后又开始折腾,Z-Turn。在Google的帮助下又让我找到光线部落的“WordPress 整合 Google Ajax 搜索”,并按照此方法成功,大家可以测试下。实现的功能如下:
- 非搜索页面纯 HTML 代码,不加载不相关资源;
- 搜索页面整合 Google 自定义搜索;
- 搜索页面中,搜索结果随文字的输入动态呈现,无需单击”搜索“。
一、申请Google自定义搜索
访问此处创建自定义搜索引擎,要搜索的网站可填入 www.example.com/*(www.example.com 为您的网站);
如果只要搜索文章页,固定链接的结尾需要有固定字符(如 .html),那么要搜索的网站可填入www.example.com/*.html。
在“外观”选项中,选择托管“搜索元素”、“两栏”布局并保存。
接下来,还可以在控制面板中修改搜索引擎的细节。尽管可以关掉广告,但该选项仅适用于已注册的非营利组织、大学和政府机构,建议不属于这些行列的博客,在“赚钱”选项中将搜索引擎与 Google Adsense 帐户关联,这样还能带来额外收入。
二、修改搜索框
不同的主题搜索框代码位置是不一样的,我的header.php 中的搜索框是这样的:
<form action=”/search” id=”searchbox”>
<input type="text" name="q" id="search-text" />
<input type="submit" value="搜索" />
</form>
关键内容是action=”/search”和name=”q”,表示会跳转到/search?q=搜索内容
三、结果页面
先在当前 WordPress 主题的目录下新建一个模板文件,文件名自定,如 cse.php,先在其中输入以下内容:
<?php /* Template Name: CSE */ ?>
<?php get_header(); ?>
// 这里插入获得的Google代码
<?php get_sidebar(); ?>
<?php get_footer(); ?>
然后进入Google自定义搜索的“获取代码”,你将得到类似的如下内容:
<div id=”cse” style=”width:100%;”>载入中……</div>
<script src=”http://www.google.com/jsapi” type=”text/javascript”></script>
<script type=”text/javascript”>
google.load(‘search’, ’1′, {language : ‘zh-CN’});
google.setOnLoadCallback(function() {
var customSearchControl = newgoogle.search.CustomSearchControl(’000000000000000000000:00000000000′);
customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
var options = new google.search.DrawOptions();
options.setSearchFormRoot(‘cse-search-form’);
options.setAutoComplete(true);
customSearchControl.draw(‘cse’, options);
}, true);
</script>
<link rel=”stylesheet” href=”http://www.google.com/cse/style/look/shiny.css” type=”text/css”/>
将其插入cse.php放Google代码处,其中000000000000000000000:00000000000 应替换为Google自定义搜索控制面板基本信息中“搜索引擎的唯一ID”。到了这一步如果你想简单点的话直接将
var search = ‘<?php echo $_GET['q'] ; ?>’; //通过php直接获得表单传入数据
// options.setSearchFormRoot(‘cse-search-form’); // Google 搜索框
options.setInput(document.getElementById(‘search-text’)); // 传递输入元素document.getElementById(‘searchbox’).setAttribute(‘onSubmit’,”document.getElementById(‘search-text’).select(); return false;”); // 按下搜索按钮选中搜索框,并阻止表单提交
document.getElementById(‘search-text’).value = search; // 设置搜索框的内customSearchControl.draw(‘cse’, options);
customSearchControl.execute(search);
这段代码替换掉后,其中search-text应替换为搜索文本框id searchbox 应替换为搜索表单id。保存cse.php。
options.setSearchFormRoot(‘cse-search-form’);
options.setAutoComplete(true);
customSearchControl.draw(‘cse’, options);
然后在Wordpress后台创建Page页面,永久地址为search,模板选择cse.php,完成后Google Ajax搜索就可以正常工作了,现在你的导航那多了一个search页,想把它隐藏的在主题模板中找到如header.php中找到<?php wp_list_pages(‘title_li=&depth=1‘); ?>这个形式的,将其改为<?php wp_list_pages(‘title_li=&depth=1&exclude=1′); ?>,其中exclude后面的数字就是你排除显示的page页面,可以在后台page页面编辑处鼠标指针放在上面找到。
最后就剩下美工了,我就用的Google自带样式,如果还想自定义请参考Google AJAX 搜索 API 开发人员指南

先占沙发再试下你的自定义google search
@vastar 欢迎测试
感觉很麻烦呢..还是用回自带的搜索,虽然功能非常地差,呵呵
过几天有时间了修改一下我的搜索
这个好像很高级的说。
回访了。。。
很少使用blog自带的搜索功能~~~
@奶牛 不一定,zww的和阿邙的我就经常用
我是用的cse内嵌到blog中,博主提供的方法尚未使用过。博客文章很棒,会常来学习的。
这两天正在研究这个,准备弄个,也写教程,哈哈。
高手
This is recognized as a wonderful article and give thank you to you for discussing this good experience,and wish you are capable to give an additional posts as quickly as possible.
Isaiah Cunningham
I favored it alout I can`t wait around till her subsequent BOOK!!!!!
The closing indicates how bust ups are not the worst factor inside the world. you can get countless a whole lot worse issues in life. including Hitler and Stalin.
Concerning perceptions, this track indicates that everybody sights issues differently. Whereas the lyrics from the track be connected to issues like tears other people could possibly not see bust ups as that huge of the offer at all. The writer likely went by means of a difficult bust up.
Mustaki v?h? tuntuu et en keksi mit??n siihen tanssiin.
I believe that was really awsome and cool.
I was searching at your weblog and I believe it of a lot interesting, and really useful, this kind of information sites i love really much, thank you for discussing with everyone!!
I believe that this track at 1st is about how delighted you might be breaking up with someone, but inside the finish you perhaps really feel bad.
Everyone thinks breakups are poor becuase they complete a huge offer about it. In simple fact all everybody wants to have more than the breakup is time.
Viel? pari p?iv?? joudutte j?nnitt??n teemaa, eik?h?n me se teille tiistaina paljasteta
I won’t abandon you*_*
Thanks for discussing your article. I actually loved it. I spot a web page web page link to my internet website to suitable right here so other individuals nowadays can look at it.
Haluisin niin kuulla sen meijan uuden teeman ja musan ois niin kiva tiet??.
I definitely such as this monitor Mr. B =D
Gutty concepts make genuinely really feel dolorous, There need to are already a kind of genuinely like that until I die, Have referred to for the purpose that united says laguna, There need to are already a kind of great agreed commonly are everlasting pleasure that know it’s genuinely superior of you.
I think I��ll give the sharks fin together with other components a miss.
Sara, okei koittakaahan saada se juttu ennen joulua mulle p?in tulemaan
The closing signifies how bust ups are not the worst element within the world. you could possibly get countless a entire whole lot even worse difficulties in life. which includes Hitler and Stalin.
You are too great a writer man. i’m in definitely like with all of your content articles or evaluations and wait around close to for them to appear. But, this articles of all that you simply just have released until now is my man or women favorite. I should really absolutely definitely maintain the exact same exact articles forwarded forward and distribute the details to one and all.
One evening a thunderstorm blew up collectively the Tennessee River. it experienced been one of people moncler decrease coat storms that make the evening go dark as correctly as humidity disappear. 1st it experienced been nevertheless and quiet. there is electric energy within the refreshing oxygen soon after which it the sharp crispness of Moncler shop on-line a summer time time evening obtaining blown broad offered due to the fact winds rushed in. We threw offered all within the doorways and windows. We found the classical r / c funnel from two low-priced north confront jackets towns apart and turned up the bass and cranked up the speakers. We allow the wind blow in and churn our summer time time evening around. We allow the audio tracks we experienced been only vaguely typical moncler decrease jacket with roar by signifies within the house. And we twirled. We twirled within the residing spot within the wind and within the music. We twirled and we imagined that individuals experienced been poets North confront jackets buy and dancers and authorities and springtime brides.
hi youthful lady fab wesite