WordPress 默认的搜索功能实在并不怎么可靠,也不是很准确,这一点相信许多朋友都清楚。网站运行时间长了,文章、页面等内容也多了起来,读者想找到自己真正想要的资源,往往需要通过关键词进行搜索。为了给你的读者提供更加准确的搜索结果,本文为你介绍10个能够明显提高 WordPress 搜索效率的实用代码,如果你想让你的读者更加容易找到你的文章,那么就请看一看,相信一定对你有所帮助。
1.从搜索结果中排除某些文章或者页面
要将你不想出现在搜索结果中的文章或页面排除掉,复制下面的代码到主题文件functions.php中即可.
function SearchFilter($query) {
if ($query->is_search) {
$query->set('cat','0,1');
}
return $query;
}
add_filter('pre_get_posts','SearchFilter');
代码中的ID号为0和1的文章将会被排除,你可以根据需要进行修改。
代码来源: wprecipes.com →
2.给WordPress添加下拉式文章分类搜索选项
这是一个比较实用的搜索功能函数,利用这条函数可以缩小搜索范围,方便读者快速获得搜索结果。
将下面的代码覆盖粘贴到searchform.php中:
<form role="search" method="get" id="searchform" action="<?php bloginfo('siteurl'); ?>">
<div>
<label class="screen-reader-text" for="s">搜索词:</label>
<input type="text" value="" name="s" id="s" />
in <?php wp_dropdown_categories( 'show_option_all=所有分类' ); ?>
<input type="submit" id="searchsubmit" value="搜索" />
</div>
</form>
看看效果图:
代码来源: www.melandri.net →
3.动态搜索文章分类和子分类
从给出的文章父分类中搜索其下的子分类,让读者获得更加精细和快速的搜索体验。代码中指定了搜索ID为1的分类,你可以根据需要进行修改。将代码覆盖粘贴到searchform.php中即可:
<form method=”get” id=”searchform” action=”<?php bloginfo(‘home’); ?>/”>
<div id=”search”>
<input type=”text” value=”Search… ” onclick=”this.value=”;” name=”s” id=”s” />
<?php $categories = get_categories(‘child_of=1′);
$catlist = ”;
foreach ($categories as $cat) {
$catlist.= $cat->cat_ID.’,';
}
$catlist.’5′;
?>
<input type=”hidden” name=”cat” value=”<?php echo “$catlist”?>” />
<input name=”" type=”image” src=”<?php bloginfo(‘stylesheet_directory’); ?>/styles/<?php echo “$style_path”; ?>/search.gif” value=”Go” class=”btn” />
</div><!–/search –>
</form>
注意:代码中有个search.gif,是搜索按钮图片,你可以自己制作一个。
代码来源: wpgarage.com →
4.搜索指定的文章类型
WordPress允许我们搜索特定类型的文章,将下面的代码粘贴到function.php文件中:
function SearchFilter($query) {
if ($query->is_search) {
// Insert the specific post type you want to search
$query->set('post_type', 'feeds');
}
return $query;
}
// This filter will jump into the loop and arrange our results before they're returned
add_filter('pre_get_posts','SearchFilter');
代码来源: ashbluewebdesign.com →
5.显示搜索结果总数
打开search.php文件,找到类似如下一句:
<h2 class="pagetitle">Search Result</h2>
然后将它替换为如下代码:
<h2 class="pagetitle">Search Result for <?php /* Search Count */ $allsearch = &new WP_Query("s=$s&showposts=-1"); $key = wp_specialchars($s, 1); $count = $allsearch->post_count; _e(''); _e('<span class="search-terms">'); echo $key; _e('</span>'); _e(' — '); echo $count . ' '; _e('articles'); wp_reset_query(); ?></h2>
代码来源: problogdesign.com →
6.使用jQuery高亮Wordpress搜索词
将下面的这段代码粘贴到你主题文件functions.php中:
function hls_set_query() {
$query = attribute_escape(get_search_query());
if(strlen($query) > 0){
echo '
<script type="text/javascript">
var hls_query? = "'.$query.'";
</script>
';
}
}
function hls_init_jquery() {
wp_enqueue_script('jquery');
}
add_action('init', 'hls_init_jquery');
add_action('wp_print_scripts', 'hls_set_query');
再将下面这一段放到header.php文件中,要放在标签</head>的前面哦:
<style type="text/css" media="screen">
<.hls { background: #D3E18A; }
</style>
<script type="text/javascript">
jQuery.fn.extend({
highlight: function(search, insensitive, hls_class){
var regex = new RegExp("(<[^>]*>)|(\\b"+ search.replace(/([-.*+?^${}()|[\]\/\\])/g,"\\$1") +")", insensitive ? "ig" : "g");
return this.html(this.html().replace(regex, function(a, b, c){
return (a.charAt(0) == "<") ? a : "<strong class=\""+ hls_class +"\">" + c + "</strong>";
}));
}
});
jQuery(document).ready(function($){
if(typeof(hls_query) != 'undefined'){
$("#post-area").highlight(hls_query, 1, "hls");
}
});
</script>
代码来源: weblogtoolscollection.com →
7.禁用WordPress搜索功能
将下面的代码放到主题文件functions.php中:
function fb_filter_query( $query, $error = true ) {
if ( is_search() ) {
$query->is_search = false;
$query->query_vars[s] = false;
$query->query[s] = false;
// to error
if ( $error == true )
$query->is_404 = true;
}
}
add_action( 'parse_query', 'fb_filter_query' );
add_filter( 'get_search_form', create_function( '$a', "return null;" ) );
代码来源: wpengineer.com →
8.让WordPress的搜索结果数目没有限制
标准的WordPress Loop对搜索结果存在一定的数目限制,通过添加以下代码我们就可以突破这个限制了.
首先在search.php中找到如下现行:
<?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?>
将它替换成如下代码:
<?php $posts=query_posts($query_string . '&posts_per_page=-1'); ?> <?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?>
代码 来源: wphacks.com →
9.Google AJAX 搜索 API 使用方法
看看演示演示→
谷歌给我们提供了一种很棒的搜索功能,可以用他们的搜索引擎来搜索我们的网站,这就是:Google AJAX 搜索API。 你需要拥有一个谷歌帐户,然后才能注册使用这个功能,你可以到这里注册: Google AJAX Search API
如果你不想看这个教程,也可以使用相关插件:http://wordpress.org/extend/plugins/google-ajax-search/
10.为WordPress添加一个基于Ajax的自动完成搜索功能
就像上图中效果那样,读者输入搜索关键词后,会弹出一个装有相关搜索条目的下拉菜单,让你可以从中选择要搜索的关键词。
11.将谷歌自定义搜索整合到 WordPress 的两种方法
![]()
在本教程中,你将能够学会如何将谷歌自定义搜索功能整合到自己的Wordpress博客之中,这样做的结果是:读者更加容易快速的搜索你的博客内容,另外,你也可以将自己的Google Adsence添加到此自定义搜索结果中,从而赚取美元。