欢迎您光临自学哈网,只为分享网络知识教程,供大家学习参考!

「自学哈网」如何增强WordPress搜索结果相关性准确度?

作者 : 自学哈 本文共2357个字,预计阅读时间需要6分钟 2022-11-26 共145人阅读
也想出现在这里? 联系我们

WordpPress 自带的搜索功能其实比较简单,搜索的结果就是包含搜索词的文章、页面,按照时间发布顺序排序,那么如何增强WordPress搜索结果相关性准确度?我们一起了解一下。

1.增加关联性

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 );

选择分类搜索:

<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>

本站声明:
本站所有资源来源于网络,分享目的仅供大家学习和交流!如若本站内容侵犯了原著者的合法权益,可联系邮箱976157886@qq.com进行删除。
自学哈专注于免费提供最新的分享知识、网络教程、网络技术的资源分享平台,好资源不私藏,大家一起分享!

自学哈网 » 「自学哈网」如何增强WordPress搜索结果相关性准确度?
也想出现在这里? 联系我们
© 2022 Theme by - 自学哈网 & WordPress Theme. All rights reserved 浙ICP备2022016594号