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