-
Notifications
You must be signed in to change notification settings - Fork 143
/
search.php
99 lines (77 loc) · 2.7 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
<?php
/**
* The template for displaying search results pages
*
* @link https://developer.wordpress.org/themes/basics/template-hierarchy/#search-result
*
* @Date: 2019-10-15 12:30:02
* @Last Modified by: Roni Laukkarinen
* @Last Modified time: 2022-02-08 17:03:23
*
* @package air-light
*/
namespace Air_Light;
$results = [];
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( ! empty( $_GET['s'] ) && have_posts() ) {
while ( have_posts() ) {
the_post();
$post_type = get_post_type();
$results[ $post_type ]['posts'][] = [
'title' => (string) get_the_title(),
'permalink' => (string) get_permalink(),
'excerpt' => (string) get_the_excerpt(),
];
}
} wp_reset_postdata();
// Get post type objects for results
foreach ( $results as $slug => $post_type ) {
$results[ $slug ]['object'] = (object) get_post_type_object( $slug );
$results[ $slug ]['count'] = (int) count( $results[ $slug ]['posts'] );
}
get_header(); ?>
<main class="site-main">
<section class="block block-search">
<div class="container">
<h1><?php echo esc_html( get_default_localization( 'Search' ) ); ?></h1>
<?php get_search_form( true ); ?>
</div>
</section>
<?php if ( ! empty( $results ) ) : ?>
<section class="block block-search-results">
<div class="container">
<?php foreach ( $results as $slug => $post_type ) : ?>
<div class="col col-results col-results-<?php echo esc_attr( $slug ) ?>">
<h2>
<?php echo esc_html( $post_type['object']->labels->name ); ?>
(<?php echo esc_html( $post_type['count'] ); ?>)
</h2>
<?php foreach ( $post_type['posts'] as $post ) : ?>
<div class="row row-result row-result-<?php echo esc_attr( $slug ) ?>">
<div class="content">
<h3>
<a href="<?php echo esc_url( $post['permalink'] ) ?>">
<?php echo esc_html( $post['title'] ) ?>
</a>
</h3>
<p><?php echo wp_kses_post( $post['excerpt'] ) ?></p>
</div>
</div>
<?php endforeach; ?>
</div>
<?php endforeach; ?>
</div>
</section>
<?php endif; ?>
<?php
// "No results" message block
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( ! empty( $_GET['s'] ) && ! have_posts() ) : ?>
<section class="block block-search-results">
<div class="container">
<h2><?php echo esc_html( get_default_localization( 'No results found for your search' ) ); ?>.</h2>
</div>
</section>
<?php endif; ?>
</main>
<?php get_footer();