`

增强WordPress的原生搜索

阅读更多

访问了很多朋友用wordpress做的博客,很多人都是用google来代替原生的搜索功能,对一些访问量比较大的博客来说,可能Wordpress内置的原生搜索是Wordpress的缺点之一。在这篇文章中,整理了一些代码片段,这会使你的Wordpress原生搜索变得好很多。

 

在搜索标题中显示标题数

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

<h1 class="search-title">Search Results</h1>

替换为

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

<h1 class="search-title">"< ?php the_search_query();?>"的搜索结果共<strong>< ?php
  global $wp_query;
  echo $wp_query->found_posts;
?></strong>篇</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',
        '<span class="search-excerpt">\0</span>',
        $title);
?>
        <h2><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>" rel="bookmark">
          <?php echo $title; ?>
          </a></h2>

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

span.search-excerpt { background: #ffc; }
当搜索结果只有一篇时直接重定向到该文章

用下面的方法可以很方便的实现当搜索结果只有一篇时直接重定向到该文章,编辑你的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_por_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 = false;
        $query->query = 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(); ?>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics