From b033571c2a0c809bf5e49fa70aa26534bd4d259d Mon Sep 17 00:00:00 2001 From: solstice23 Date: Sun, 5 Dec 2021 01:49:12 +0800 Subject: [PATCH] support search filter --- argontheme.js | 47 +++++++++++++++++++----- functions.php | 49 +++++++++++++++++++++++++- search.php | 28 +++++++++++++-- style.css | 7 ++++ template-parts/content-none-search.php | 6 +++- 5 files changed, 125 insertions(+), 12 deletions(-) diff --git a/argontheme.js b/argontheme.js index 7e193726..a8acb93c 100644 --- a/argontheme.js +++ b/argontheme.js @@ -288,6 +288,20 @@ function __(text){ document.addEventListener("scroll", changeToolbarTransparency, {passive: true}); }(); +/*搜索*/ +function searchPosts(word){ + if ($(".search-result").length > 0){ + let url = new URL(window.location.href); + url.searchParams.set("s", word); + $.pjax({ + url: url.href + }); + }else{ + $.pjax({ + url: argonConfig.wp_path + "?s=" + encodeURI(word) + }); + } +} /*顶栏搜索*/ $(document).on("click" , "#navbar_search_input_container" , function(){ $(this).addClass("open"); @@ -306,9 +320,7 @@ $(document).on("keydown" , "#navbar_search_input_container #navbar_search_input" return; } let scrolltop = $(document).scrollTop(); - $.pjax({ - url: argonConfig.wp_path + "?s=" + encodeURI(word) - }); + searchPosts(word); }); /*顶栏搜索 (Mobile)*/ $(document).on("keydown" , "#navbar_search_input_mobile" , function(e){ @@ -321,9 +333,7 @@ $(document).on("keydown" , "#navbar_search_input_mobile" , function(e){ return; } let scrolltop = $(document).scrollTop(); - $.pjax({ - url: argonConfig.wp_path + "?s=" + encodeURI(word) - }); + searchPosts(word); }); /*侧栏搜索*/ $(document).on("click" , "#leftbar_search_container" , function(){ @@ -347,8 +357,27 @@ $(document).on("keydown" , "#leftbar_search_input" , function(e){ return; } $("html").removeClass("leftbar-opened"); + searchPosts(word); +}); +/*搜索过滤器*/ +$(document).on("change" , ".search-filter" , function(e){ + if (pjaxLoading){ + $(this).prop("checked", !$(this).prop("checked")); + e.preventDefault(); + return; + } + pjaxLoading = true; + let postTypes = []; + $(".search-filter:checked").each(function(){ + postTypes.push($(this).attr("name")); + }); + if (postTypes.length == 0){ + postTypes = ["none"]; + } + let url = new URL(document.location.href); + url.searchParams.set("post_type", postTypes.join(",")); $.pjax({ - url: argonConfig.wp_path + "?s=" + encodeURI(word) + url: url.href }); }); @@ -1720,7 +1749,7 @@ if ($("html").hasClass("banner-as-cover")){ } /*Pjax*/ -var pjaxScrollTop = 0; +var pjaxScrollTop = 0, pjaxLoading = false; $.pjax.defaults.timeout = 10000; $.pjax.defaults.container = ['#primary', '#leftbar_part1_menu', '#leftbar_part2_inner', '.page-information-card-container', '#wpadminbar']; $.pjax.defaults.fragment = ['#primary', '#leftbar_part1_menu', '#leftbar_part2_inner', '.page-information-card-container', '#wpadminbar']; @@ -1732,6 +1761,7 @@ $(document).pjax("a[href]:not([no-pjax]):not(.no-pjax):not([target='_blank']):no } NProgress.remove(); NProgress.start(); + pjaxLoading = true; }).on('pjax:afterGetContainers', function(e, f, g) { if (g.is("#main article.post-preview a.post-title")){ let $card = $(g.parents("article.post-preview")[0]); @@ -1776,6 +1806,7 @@ $(document).pjax("a[href]:not([no-pjax]):not(.no-pjax):not([target='_blank']):no } } }).on('pjax:complete', function() { + pjaxLoading = false; NProgress.inc(); try{ if (MathJax != undefined){ diff --git a/functions.php b/functions.php index f21f58c7..052d22ea 100644 --- a/functions.php +++ b/functions.php @@ -2030,7 +2030,6 @@ function update_post_meta_ajax(){ return; } - $result = update_post_meta($post_id, $meta_key, $meta_value); if ($result){ @@ -4280,6 +4279,19 @@ function themeoptions_page(){

+ + + + +

+ + @@ -4726,6 +4738,7 @@ function argon_update_themeoptions(){ argon_update_option('argon_comment_pagination_type'); argon_update_option('argon_who_can_visit_comment_edit_history'); argon_update_option('argon_home_show_shuoshuo'); + argon_update_option('argon_search_post_filter'); argon_update_option('argon_darkmode_autoswitch'); argon_update_option('argon_enable_amoled_dark'); argon_update_option('argon_outdated_info_time_type'); @@ -4857,6 +4870,40 @@ function init_shuoshuo(){ register_post_type('shuoshuo', $args); } +function argon_get_search_post_type_array(){ + $search_filter_option = get_option('argon_search_post_filter', 'post,page'); + if (!isset($_GET['post_type'])) { + if ($search_filter_option == 'off'){ + return array('post', 'page'); + } + $default = explode(',', $search_filter_option); + return $default; + } + $post_type = $_GET['post_type']; + $arr = array(); + if (strpos($post_type, 'post') !== false) { + array_push($arr, 'post'); + } + if (strpos($post_type, 'page') !== false) { + array_push($arr, 'page'); + } + if (strpos($post_type, 'shuoshuo') !== false && !in_array('hide_shuoshuo', explode(',', $search_filter_option))) { + array_push($arr, 'shuoshuo'); + } + if (count($arr) == 0) { + array_push($arr, 'none'); + } + return $arr; +} +function search_filter($query) { + if (!$query -> is_search) { + return $query; + } + $query -> set('post_type', argon_get_search_post_type_array()); + return $query; +} +add_filter('pre_get_posts', 'search_filter'); + /*恢复链接管理器*/ add_filter('pre_option_link_manager_enabled', '__return_true'); diff --git a/search.php b/search.php index 38a43a57..865ecc44 100644 --- a/search.php +++ b/search.php @@ -1,12 +1,36 @@
-
+
>

+ +
+
+ > + +
+
+ > + +
+ +
+ > + +
+ +
+ +

found_posts; ?> @@ -18,7 +42,7 @@

-
+
.tippy-svg-arrow { margin: 30px 20px; margin-top: 0px; } +.search-filter-wrapper { + display: inline-block; + margin-right: 12px; +} +.search-filter-wrapper > .custom-control-label { + line-height: 25px; +} /*文章导航 (上/下一篇文章)*/ .post-navigation { diff --git a/template-parts/content-none-search.php b/template-parts/content-none-search.php index 57f6e1bd..f08bec16 100644 --- a/template-parts/content-none-search.php +++ b/template-parts/content-none-search.php @@ -2,7 +2,11 @@

- + + + + +