1
0

search.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * The template for displaying search results pages.
  4. *
  5. * @link https://developer.wordpress.org/themes/basics/template-hierarchy/#search-result
  6. *
  7. * @package Sakurairo
  8. */
  9. get_header(); ?>
  10. <section id="primary" class="content-area">
  11. <main id="main" class="site-main" role="main">
  12. <?php
  13. $paged = max(1, get_query_var('paged'));
  14. $search_query = get_search_query();
  15. $sticky_posts = get_option('sticky_posts');
  16. //搜索页标题
  17. if (!iro_opt('patternimg') || !get_random_bg_url()) : ?>
  18. <header class="page-header">
  19. <h1 class="page-title"><?php printf(esc_html__('Search result: %s', 'sakurairo'), '<span>' . esc_html($search_query) . '</span>'); ?></h1>
  20. </header><!-- .page-header -->
  21. <?php endif;
  22. //结果排序方式
  23. $all_results_args = array(
  24. 'post_type' => array('post', 'shuoshuo'),
  25. 'post_status' => 'publish',
  26. 's' => $search_query,
  27. 'posts_per_page' => -1,
  28. 'orderby' => 'relevance',
  29. 'order' => 'DESC',
  30. );
  31. $all_results_query = new WP_Query($all_results_args);
  32. //结果
  33. $all_results = [];
  34. $sticky_results = [];
  35. $non_sticky_results = [];
  36. //分类置顶内容和非置顶内容
  37. if ($all_results_query->have_posts()) :
  38. while ($all_results_query->have_posts()) : $all_results_query->the_post();
  39. if (in_array(get_the_ID(), $sticky_posts)) {
  40. $sticky_results[] = $post;
  41. } else {
  42. $non_sticky_results[] = $post;
  43. }
  44. endwhile;
  45. endif;
  46. wp_reset_postdata();
  47. //合并结果,优先展示置顶文章
  48. $all_results = array_merge($sticky_results, $non_sticky_results);
  49. // 内容分页
  50. $total_results = count($all_results);
  51. $posts_per_page = 10;
  52. $total_pages = ceil($total_results / $posts_per_page);
  53. $current_page_results = array_slice($all_results, ($paged - 1) * $posts_per_page, $posts_per_page);
  54. //输出当前页内容
  55. if (!empty($current_page_results)) :
  56. foreach ($current_page_results as $post) :
  57. setup_postdata($post);
  58. get_template_part('tpl/content', 'thumbcard');
  59. endforeach;
  60. //分页跳转
  61. the_posts_pagination(array(
  62. 'total' => $total_pages,
  63. 'current' => $paged,
  64. ));
  65. else :
  66. //未找到搜索结果
  67. ?>
  68. <div class="search-box">
  69. <!-- search start -->
  70. <form class="s-search">
  71. <input class="text-input" type="search" name="s" placeholder="<?php esc_attr_e('Search...', 'sakurairo'); ?>" required>
  72. </form>
  73. <!-- search end -->
  74. </div>
  75. <?php get_template_part('tpl/content', 'none'); ?>
  76. <?php
  77. endif;
  78. wp_reset_postdata();
  79. ?>
  80. <style>
  81. .nav-previous,
  82. .nav-next {
  83. padding: 20px 0;
  84. text-align: center;
  85. margin: 40px 0 80px;
  86. display: inline-block;
  87. font-family: 'Fira Code', 'Noto Sans SC';
  88. }
  89. .nav-previous a,
  90. .nav-next a {
  91. padding: 13px 35px;
  92. border: 1px solid #D6D6D6;
  93. border-radius: 50px;
  94. color: #ADADAD;
  95. text-decoration: none;
  96. }
  97. .nav-previous span,
  98. .nav-next span {
  99. color: #989898;
  100. font-size: 15px;
  101. }
  102. .nav-previous a:hover,
  103. .nav-next a:hover {
  104. border: 1px solid #A0DAD0;
  105. color: #A0DAD0;
  106. }
  107. </style>
  108. </main><!-- #main -->
  109. </section><!-- #primary -->
  110. <?php get_footer(); ?>