足球游戏_中国足彩网¥体育资讯$

wp博客无缝整合google自定义搜索框
来源:易贤网 阅读:929 次 日期:2016-06-25 10:57:10
温馨提示:易贤网小编为您整理了“wp博客无缝整合google自定义搜索框”,方便广大网友查阅!

今天我给大家分享的是无缝整合google自定义搜索框的技巧。早在08年denis就写过一篇整合google自定义搜索到wordpress中的教程,可以达到强化搜索、减轻数据库读取和赚取利润的各种好处。其中的第6步是用google 的搜索框代替主题本身的搜索框,但是现在使用国外主题和付费主题的朋友越来越多了,这类主题都有一共同效果——界面ui棒!拥有精美搜索框的主题也不在少数,如果让你放弃原先精美的搜索框,而用 google 那简单单一的搜索框是不是会有点不舍呢?

不用担心,接下来 packy 教你一步步无缝整合google 自定义搜索框,可以在不修改原搜索框的前提下使用 google 强大的自定义搜索功能。

如果你是第一次整合google自定义搜索,可以按照我的步骤来;如果你对代码较了解,可以根据你的需要选择性的看。

第一步:注册并获取 google 自定义搜索代码

整合 google 自定义搜索之前肯定必须要先让 google 为你服务,通过访问http://www.google.com/cse/ 创建你的搜索引擎。创建完毕后进入“外观”面板,选择“全宽”的布局模式。保存后进入“获取代码”,获得你的 google 自定义搜索代码:

<!-- put the following javascript before the closing </head> tag. -->

<script>

(function() {

var cx = '015818537936328944739:nkbsvpppu5k';

var gcse = document.createelement('script'); gcse.type = 'text/javascript'; gcse.async = true;

gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +

'//www.google.com/cse/cse.js?cx=' + cx;

var s = document.getelementsbytagname('script')[0]; s.parentnode.insertbefore(gcse, s);

})();

</script>

<!-- place this tag where you want both of the search box and the search results to render -->

<gcse:search></gcse:search>

先保留着这些代码,不用理他,继续第二步

第二步:创建搜索结果页

为了让搜索结果在博客内部显示,我们需要在 wordpress 中创建一个新的页面,用来显示搜索的搜索结果。我们在本地新建一个文件,命名为 search.php,文件内容复制下面的即可:

<?php

/*

template name: search

*/

?>

<?php get_header(); ?>

<div id=cse style=width: 100%;>loading</div>

<script src=http://www.google.com.hk/jsapi type=text/javascript></script>

<script type=text/javascript>

google.load('search', '1', {language : 'zh-cn'});

google.setonloadcallback(function(){

var customsearchcontrol = new google.search.customsearchcontrol('你的google自定义搜索id');

customsearchcontrol.setresultsetsize(google.search.search.filtered_cse_resultset);

customsearchcontrol.draw('cse');

});

</script>

<link rel=stylesheet href=http://www.google.com.hk/cse/style/look/shiny.css type=text/css />

<?php get_footer(); ?>

其中将“你的 google 自定义搜索 id”替换为 google 给你的“搜索引擎的唯一 id”,可以在控制面板的基本信息内获取。

保存后将 search.php 上传至你的主题根目录下。

最后在你博客的后台 – 页面中新建页面,在页面属性的模版中找到 search 并选择,写好标题发布即可。

第三步:修改当前主题的搜索提交的表单

这里算是最关键的一步啦,就是当用户点击你博客上任意页面的站内搜索按钮的时候,将用户引导到你刚刚创建的搜索结果页上。这里我们需要在主题文件夹中找到搜索框所在的文件,每个主题都不同,我用我在使用的一款主题来演示吧,找到类似以下的代码:

<form method=get action=/search?>

<input type=text size=24 name=s value=在wpzti.com中尽情搜索吧 class=textfield style=float:left onblur=if (this.value == ”) {this.value = ‘在wpzti.com中尽情搜索吧’;} onfocus=if (this.value != ”) {this.value = ”;}/?>

<input class=submitsearch type=submit value=search?>?</input?>

</form?>

其中我们需要修改的地方大致如下:

method=”get”

action=”/search”

还有文本框 name=”q”

*action 的地址可以根据你自己固定链接的方式来修改,只要保证能访问到我们刚新建的页面就行;不管你原先主题搜索框的 name 等于什么,都将引号内的字母改成 q。

第四步:初始化搜索关键字

这是无缝整合 google 自定义搜索框的最后一步,完成他你就大功告成了哦。这一步我们要做的是:从 url 中提取浏览者搜索的关键词,然后调用 google api 进行搜索。听起来很复杂?无需理解,简单的跟着做就可以了:

打开我们刚才新建的 search.php,在 google 的代码

customsearchcontrol.draw(‘cse’, options);

后插入以下代码:

var match = location.search.match(/q=([^&]*)(&|$)/);

if(match && match[1]){

var search = decodeuricomponent(match[1]);

customsearchcontrol.execute(search);

}

大功告成啦,从此以后你依旧可以使用主题原始的搜索框而享受 google 自定义搜索带来的好处。

中国足彩网信息请查看CMS教程
易贤网手机网站地址:wp博客无缝整合google自定义搜索框
由于各方面情况的不断调整与变化,易贤网提供的所有考试信息和咨询回复仅供参考,敬请考生以权威部门公布的正式信息和咨询为准!
关于我们 | 联系我们 | 人才招聘 | 网站声明 | 网站帮助 | 非正式的简要咨询 | 简要咨询须知 | 加入群交流 | 手机站点 | 投诉建议
工业和信息化部备案号:滇ICP备2023014141号-1 足球游戏_中国足彩网¥体育资讯$ 滇公网安备53010202001879号 人力资源服务许可证:(云)人服证字(2023)第0102001523号
云南网警备案专用图标
联系电话:0871-65317125(9:00—18:00) 获取招聘考试信息及咨询关注公众号:hfpxwx
咨询QQ:526150442(9:00—18:00)版权所有:易贤网
云南网警报警专用图标