feat: search function enhancement (#338)

pull/343/head
Seaton Jiang 2021-03-10 09:56:52 +08:00
parent a35be1d185
commit 35724c0173
No known key found for this signature in database
GPG Key ID: C1086BAE716FF138
3 changed files with 36 additions and 6 deletions

View File

@ -251,3 +251,35 @@ function custom_upload_filter($file)
return $file;
}
// 仅搜索文章标题
if (kratos_option('g_search', false)) {
add_filter('posts_search', 'search_enhancement', 10, 2);
function search_enhancement($search, $wp_query)
{
if (!empty($search) && !empty($wp_query->query_vars['search_terms']))
{
global $wpdb;
$q = $wp_query->query_vars;
$n = !empty($q['exact']) ? '' : '%';
$search = array();
foreach ((array)$q['search_terms'] as $term)
{
$search[] = $wpdb->prepare("$wpdb->posts.post_title LIKE %s", $n . $wpdb->esc_like( $term ) . $n);
}
if (!is_user_logged_in())
{
$search[] = "$wpdb->posts.post_password = ''";
}
$search = ' AND ' . implode(' AND ', $search);
}
return $search;
}
}

View File

@ -112,9 +112,9 @@ function kratos_options()
);
$options[] = array(
'name' => __('多人模式', 'kratos'),
'desc' => __('在文章列表显示当前文章作者,在文章页面页脚显示当前作者介绍', 'kratos'),
'id' => 'multiusers',
'name' => __('搜索增强', 'kratos'),
'desc' => __('仅查找文章标题,而不全文搜索(适用于文章数量较多的站点)', 'kratos'),
'id' => 'g_search',
'type' => 'checkbox',
);

View File

@ -45,9 +45,7 @@
<span class="float-left d-block">
<span class="mr-2"><i class="kicon i-hot"></i><?php echo get_post_views(); _e('点热度', 'kratos'); ?></span>
<span class="mr-2"><i class="kicon i-good"></i><?php if (get_post_meta($post->ID, 'love', true)) {echo get_post_meta($post->ID, 'love', true);} else {echo '0';} _e('人点赞', 'kratos'); ?></span>
<?php if(kratos_option('multiusers',false)){ ?>
<span class="mr-2"><i class="kicon i-author"></i><?php echo get_the_author_meta('display_name'); ?></span>
<?php } ?>
<span class="mr-2"><i class="kicon i-author"></i><?php echo get_the_author_meta('display_name'); ?></span>
</span>
<span class="float-right">
<a href="<?php the_permalink(); ?>"><?php _e('阅读全文', 'kratos'); ?><i class="kicon i-rightbutton"></i></a>