WordPress教程

增强 WordpPress 搜索结果的相关性准确度

阿里云

WordpPress 自带的搜索功能其实比较简单,搜索的结果就是包含搜索词的文章、页面,按照时间发布顺序排序,下面,我们将介绍如何增强 WordPress 的搜索,让搜索结果更加精准。

1.增加关联性

  1.     if(is_search()){
  2.     add_filter('posts_orderby_request', 'search_orderby_filter');
  3.     }
  4.     function search_orderby_filter($orderby = ''){
  5.         global $wpdb;
  6.         $keyword = $wpdb->prepare($_REQUEST['s']);
  7.         return "((CASE WHEN {$wpdb->posts}.post_title LIKE '%{$keyword}%' THEN 2 ELSE 0 END) + (CASE WHEN {$wpdb->posts}.post_content LIKE '%{$keyword}%' THEN 1 ELSE 0 END)) DESC,
  8.     {$wpdb->posts}.post_modified DESC, {$wpdb->posts}.ID ASC";
  9.     }
也想出现在这里?联系我们
创客主机

只搜索文章

只搜索文章的标题,将下面的代码添加到主题的 functions.php 文件即可:

  1.     /**
  2.      * 让 WordPress 只搜索文章的标题
  3.      */
  4.     function __search_by_title_only( $search, &$wp_query )
  5.     {
  6.         global $wpdb;
  7.         if ( emptyempty( $search ) )
  8.             return $search; // skip processing - no search term in query
  9.         $q = $wp_query->query_vars;
  10.         $n = ! emptyempty( $q['exact'] ) ? '' : '%';
  11.         $search =
  12.         $searchand = '';
  13.         foreach ( (array) $q['search_terms'] as $term ) {
  14.             $term = esc_sql( like_escape( $term ) );
  15.             $search .= "{$searchand}($wpdb->posts.post_title LIKE '{$n}{$term}{$n}')";
  16.             $searchand = ' AND ';
  17.         }
  18.         if ( ! emptyempty( $search ) ) {
  19.             $search = " AND ({$search}) ";
  20.             if ( ! is_user_logged_in() )
  21.                 $search .= " AND ($wpdb->posts.post_password = '') ";
  22.         }
  23.         return $search;
  24.     }
  25.     add_filter( 'posts_search', '__search_by_title_only', 500, 2 );

选择分类搜索:

  1.     <form id=”searchform” name=”searchform” method=”get” action=”<?php bloginfo(‘home’); ?>/” >
  2.     <ul>
  3.     <li>
  4.     <p>
  5.     <?php $select = wp_dropdown_categories(class=search_select&show_option_all=全站搜索& amp;orderby=name&hierarchical=0&selected=-1&depth=1);?>
  6.     </p>
  7.     </li>
  8.     <li>
  9.     <input type=”text” name=”s” id=”s” maxlength=”34″ value=””/>
  10.     </li>
  11.     <li>
  12.     <input type=”image” value=”” src=”<?php bloginfo(‘template_url’); ?>/img/search.gif”/>
  13.     </li>
  14.     </ul>
  15.     </form>

显示出的效果类似下图功能,可选择全站搜索,或者具体的分类搜索,搜索的结果更加精准!

具体样式得自己修改了。

多重选项框搜索

这种方法更加强大,可以选择多个分类,并搜索分类中的文章,精确度更好,不过使用这种方法有一定的固定性,你需要自己写好选项框中的分类 id:

将你的默认的 searchform.php 修改为以下代码:

  1.     <div>  
  2.     <form id=”index_search” name=”index_search” method=”get” action=”<?php bloginfo(‘home’); ?>/”>  
  3.     <p><input type=”text” name=”s” id=”s” value=””/> <input type=”submit” value=” 搜 索 ” /></p>  
  4.     <p>  
  5.     <label for=”s_type5″ style=”width:50px”><input type=”radio” name=”cat” id=”cat” value=”all” checked>全站</label>  
  6.     <label for=”s_type1″ style=”width:50px”><input type=”radio” name=”cat” id=”cat” value=”4″ checked>主题</label>  
  7.     <label for=”s_type2″ style=”width:50px”><input type=”radio” name=”cat” id=”cat” value=”6″>插件</label>  
  8.     <label for=”s_type3″ style=”width:50px”><input type=”radio” name=”cat” id=”cat” value=”3″>主机</label>  
  9.     <label for=”s_type4″ style=”width:50px”><input type=”radio” name=”cat” id=”cat” value=”10″>经验</label>  
  10.     </p>  
  11.     </form>  
  12.     </div>

现在,你的 wordpress 搜索是不是强大了许多呢? 如果不想要使用 wordpress 的搜索,还可以内置谷歌搜索、百度站内搜索等方式,这些方式可以参阅谷歌百度的官网。

增强 WordpPress 搜索结果的相关性准确度

已有 210 人购买
查看演示升级 VIP立刻购买

收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!