diff --git a/inc/theme-core.php b/inc/theme-core.php index 17fb4e5..00c599f 100644 --- a/inc/theme-core.php +++ b/inc/theme-core.php @@ -250,4 +250,36 @@ 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; + } } \ No newline at end of file diff --git a/inc/theme-options.php b/inc/theme-options.php index ec7a7d9..4ba6cd3 100644 --- a/inc/theme-options.php +++ b/inc/theme-options.php @@ -112,9 +112,9 @@ function kratos_options() ); $options[] = array( - 'name' => __('多人模式', 'kratos'), - 'desc' => __('在文章列表显示当前文章作者,在文章页面页脚显示当前作者介绍', 'kratos'), - 'id' => 'multiusers', + 'name' => __('搜索增强', 'kratos'), + 'desc' => __('仅查找文章标题,而不全文搜索(适用于文章数量较多的站点)', 'kratos'), + 'id' => 'g_search', 'type' => 'checkbox', ); diff --git a/pages/page-content.php b/pages/page-content.php index 1aeb289..38c4c73 100644 --- a/pages/page-content.php +++ b/pages/page-content.php @@ -45,9 +45,7 @@ ID, 'love', true)) {echo get_post_meta($post->ID, 'love', true);} else {echo '0';} _e('人点赞', 'kratos'); ?> - - - +