WordPress添加google自定义搜索引擎

怎么在google上生成代码就不说了,这里说的是如何在wp上添加代码(我选的是两段代码的搜索)。

1.设置搜索框代码。把搜索框代码粘贴到模板的searchform.php文件里,完全覆盖源代码,代码如下:

<form action=”http://www.sharphe.cn/index.php/search” id=”cse-search-box”>
<div>
<input type=”hidden” name=”cx” value=”013103211604251038584:rt1jt7sz9fy” />
<input type=”hidden” name=”cof” value=”FORID:11″ />
<input type=”hidden” name=”ie” value=”UTF-8″ />
<input type=”text” name=”q” size=”31″ />
<input type=”submit” name=”sa” value=”GO” />
</div>
</form>
<script type=”text/javascript” src=”http://www.google.com/coop/cse/brand?form=cse-search-box&lang=zh-Hans”></script>

如果不想更改原来的搜索框格式,只需将原来代码里没有的而google代码里有的增加进来就可以了;

2.设置搜索结果页。复制一份模板文件夹里的page.php文件,改文件名为GoogleSearch.php,在最开始处添加如下代码:

<?php
/*
Template Name: Google自定义搜索结果
*/
?>

然后找到类似如下的代码:

<div>
<?php the_content(‘Continue reading &raquo;’); ?>
</div><!–/entry –>

将其替换成:

<div id=”cse-search-results”></div>
<script type=”text/javascript”>
var googleSearchIframeName = “cse-search-results”;
var googleSearchFormName = “cse-search-box”;
var googleSearchFrameWidth = 600;
var googleSearchDomain = “www.google.com”;
var googleSearchPath = “/cse”;
</script>
<script type=”text/javascript” src=”http://www.google.com/afsonline/show_afs_search.js”></script>

调整var googleSearchFrameWidth = 600;这句,使得搜索结果适合于你页面的大小;

3.在后台新建页面“站内搜索”,缩略名为search(需要伪静态),在页面模板里选择“Google自定义搜索结果”,保存即可。

其它细节刚面的就不介绍了,大家可以在网上搜索,这里需要注意的是,在Google里设置的时候,网站网址一定要写正确,否则搜索结果将不显示。

This entry was posted in wordpress and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>