首页 » 开源/框架 » WordPress » 正文

增强WordPress的原生搜索

550
对一些访问量比较大的博客来说,可能Wordpress内置的原生搜索是Wordpress的缺点之一,这边文章中,我整理了一些代码片段,这会使你的Wordpress原生搜索变得好很多。

在搜索标题中显示标题数

默认的搜索结果中是不会显示结果文章数的。可能对一些人来说会需要这个结果数,要在搜索结果中显示文章数,很简单,你只需要编辑search.php
找到

<h1>Search Results</h1>

替换为

<h1><?php 
/* Search Count */ 
$allsearch = &new WP_Query("s=$s&showposts=-1"); 
$key = wp_specialchars($s, 1); 
$count = $allsearch->post_count; _e(''); 
_e('<span>'); 
echo $key; _e('</span>的搜索结果'); _e(' — '); 
echo $count . ' '; _e('篇文章'); 
wp_reset_query(); ?></h1>

在搜索列表文章标题中高亮搜索文本

这会让你的搜索结果更加友好:高亮搜索文文
依然编辑search.php,找到你的文章输出loop

<h2><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>" rel="bookmark">
          <?php the_title(); ?>
          </a></h2>

替换为

<?php
    $title = get_the_title();
    $keys= explode(" ",$s);
    $title = preg_replace('/('.implode('|', $keys) .')/iu',
        '<strong class="search-excerpt">\0</strong>',
        $title);
?>
        <h2><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>" rel="bookmark">
          <?php echo $title; ?>
          </a></h2>

然后在你的css中加入下面的样式

strong.search-excerpt { background: yellow; }

当搜索结果只有一篇时直接重定向到该文章

用下面的方法可以很方便的实现当搜索结果只有一篇时直接重定向到该文章,编辑你的functions.php并加入以下代码

add_action('template_redirect', 'redirect_single_post');
function redirect_single_post() {
    if (is_search()) {
        global $wp_query;
        if ($wp_query->post_count == 1) {
            wp_redirect( get_permalink( $wp_query->posts['0']->ID ) );
        }
    }
}

更改Wordpress搜索结果每页显示文章数

WordPress默认的搜索结果是每页显示十篇文章,如果你想更改的话,只需要把下面的代码加到functions.php中并修改数量。

function limit_posts_per_search_page() {
    if ( is_search() )
        set_query_var('posts_per_archive_page', 20); 
}

add_filter('pre_get_posts', 'limit_posts_per_search_page');

搜索结果限制文章格式

如果你的主题支持多种文章格式并且你想在搜索结果中只输出一种格式,你只需要把下面的代码放到functions.php,并修改你想要显示的文章格式名称

function SearchFilter($query) {
  if ($query->is_search) {
    // 输入你想要显示的文章格式
    $query->set('post_type', 'feeds');
  }
  return $query;
}
add_filter('pre_get_posts','SearchFilter');

只搜索指定分类

做到这个很简单,只需要修改下面代码的分类ID号并加入到search.php中

<?php if( is_search() )  :
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts("s=$s&paged=$paged&cat=1,2,3");
endif; ?>

完全禁用搜索功能

虽然搜索是个很有用的功能,但是有时候强迫症的你就是想禁用它,那么你只需要把下面的代码放到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;" ) );

在一页中显示所有搜索结果

前面已经提到,默认搜索结果每页显示10篇,如果你想让结果在一页里显示,只需要编辑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(); ?>

英文原文地址:Hacks and snippets to enhance WordPress search engine

原文链接:http://fatesinger.com/1360.html

本文共 4 个回复

  • cy 2014/04/15 14:20

    阅。

  • Zouming 2014/09/02 09:05

    不错,就是需要这样可以用代码实现又并不太复杂的。谢谢分享!

  • Zouming 2014/09/02 09:47

    高亮显示搜索文本的,我在应用时遇到些问题:strong.search-excerpt { background: yellow; }这个无效,我在strong标签内加上了class="search-excerpt"后再.search-excerpt { background: yellow; }, 还有个严重问题,搜索高亮是实现了,但是文章列表标题却乱码了,我看了下我主题中本来是用的the_title();我换成这个后高亮显示就不生效了

    • Specs 2014/09/02 11:52

      @ Zouming get_the_title()获取到标题不会输出,而你那个 the_title() 直接把标题输出了,所以下面的就没用了,上面那个是忘了加class属性,我修改下~

发表评论