您的位置: 主页 > 博客cms > wordpress搜索功能怎么才能增强

wordpress搜索功能怎么才能增强

发布时间:2023-08-04 11:06:37 | 发布者:往流科技

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

WordPress本身的搜索结果是按照发布时间排序的,这样的搜索结果的相关性并不强,应该让搜索结果按照内容相关性排序,而不是按照时间或者 ID,所以我们可以在当前主题的 functions.php 添加如下代码来增强 WordPress 搜索的相关性:

if(is_search()){add_filter('posts_orderby_request',?'search_orderby_filter');}function?search_orderby_filter($orderby?=?''){????global?$wpdb;????$keyword?=?$wpdb->prepare($_REQUEST['s']);????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,{$wpdb->posts}.post_modified?DESC,?{$wpdb->posts}.ID?ASC";}

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

/**?*?让?WordPress?只搜索文章的标题?*/function?__search_by_title_only(?$search,?&$wp_query?){????global?$wpdb;????if?(?emptyempty(?$search?)?)????????return?$search;?//?skip?processing?-?no?search?term?in?query????$q?=?$wp_query->query_vars;????$n?=?!?emptyempty(?$q['exact']?)???''?:?'%';????$search?=????$searchand?=?'';????foreach?(?(array)?$q['search_terms']?as?$term?)?{????????$term?=?esc_sql(?like_escape(?$term?)?);????????$search?.=?"{$searchand}($wpdb->posts.post_title?LIKE?'{$n}{$term}{$n}')";????????$searchand?=?'?AND?';????}????if?(?!?emptyempty(?$search?)?)?{????????$search?=?"?AND?({$search})?";????????if?(?!?is_user_logged_in()?)????????????$search?.=?"?AND?($wpdb->posts.post_password?=?'')?";????}????return?$search;}add_filter(?'posts_search',?'__search_by_title_only',?500,?2?);

选择分类搜索文章:

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

<form?id=”searchform”?name=”searchform”?method=”get”?action=”<?php?bloginfo(‘home’);??>/”?><ul><li><p><?php?$select?=?wp_dropdown_categories(‘class=search_select&show_option_all=全站搜索&?amp;orderby=name&hierarchical=0&selected=-1&depth=1′);?></p></li><li><input?type=”text”?name=”s”?id=”s”?maxlength=”34″?value=””/></li><li><input?type=”image”?value=””?src=”<?php?bloginfo(‘template_url’);??>/img/search.gif”/></li></ul></form>

可选择全站搜索,或者具体的分类搜索,搜索的结果更加精准!

具体样式得自己修改了

多重选项框搜索

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

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

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

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