WordPress添加并整合Google自定义搜索实现步骤及代码
<p><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>给博客添加一个Google自定义搜索是十分必要的,用户搜索时高亮显示了关键词,提升了搜索的准确度,使用Google高速Ajax加载,大大减轻了主机负担,甚至你还可以自定义CSS,自定义显示结果,同时还可以在Google自定义搜索中集成Adsense与Analytics。</span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>给WordPress添加Google自定义搜索很简单,甚至有些WordPress主题已经提供了Google自定义搜索,你只要填写上Google自定义搜索的CX值即可。但显示搜索结果的页面都是在单独的窗口中打开,没有了原窗口的样式,所以我们可以将Google自定义搜索整合到博客中。</span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>由于各种主题的添加方法不同,这里只说一下比较简单的方法:网上大多数是自己创建一个cse.php文件来单独的控制CSS和填写Google自定义搜索代码,但我觉得直接把Google自定义搜索代码添加到搜索页模板中,要更加简单,大多数情况下你你基本不需要在去定义CSS。 </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>1、用 Google帐号登录 http://www.google.com/cse/ </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>2、创建一个 自定义搜索引擎,填写相关信息,选择标准版,搜索网站 获得自定义搜索代码,添加在你的主题搜索页中,一般是search.php 。(把search.php中原文件删除,因为一般是调用存档页,不然你添加后会同时显示原来搜索的内容和Google自定义搜索的内容,可能导致页面错位) </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><strong style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>注意</strong><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>:把Google自定义代码添加在这里,但要删除Google自定义搜索代码的后三行,如图所示: </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><img title="WordPress添加并整合Google自定义搜索实现步骤及代码" alt="WordPress添加并整合Google自定义搜索实现步骤及代码" src="https://zhuji.jb51.net/uploads/img/202305/d0dd30c91cd3bf5a55460d9a218ffab0.jpg" style="max-width:100%!important;height:auto!important;border: 1px solid rgb(204, 204, 204); vertical-align: middle; padding: 1px; overflow: hidden; max-width: 696px; font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px; width: 547px; height: 236px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'> </span></p>
<div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<p style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span></p>
<p>
代码如下:</p>
</div>
<p style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
<br>
<?php <br>
/* <br>
google seach <br>
*/ <br>
?> <br>
<?php get_header(); ?> <br>
<div> <br>
<div>&#160;&#160; Google自定义搜索代码&#160;&#160; var match = location.search.match(/s=([^&amp;]*)(&amp;|$)/);//正则传参 <br>
if(match &amp;&amp; match){ <br>
var search = decodeURIComponent(match); <br>
customSearchControl.execute(search); <br>
} <br>
}); <br>
</script> <br>
<link rel=&quot;stylesheet&quot; href=&quot;http://www.google.com/cse/style/look/default.css&quot; type=&quot;text/css&quot; /> <br>
</div> <br>
</div> <br>
<?php get_footer(); ?> </p>
<p>
<br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>相比而言,这种方法要简单多了,有兴趣的朋友可以试试看。</span></p>
頁:
[1]