-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.php
134 lines (125 loc) · 5.31 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
130
131
132
133
134
<?php get_header(); ?>
<?php
$search_query = get_search_query();
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$posts_per_page = 16;
$list_post_args = array(
's' => $search_query,
'paged' => $paged,
'post_status' => 'publish',
'suppress_filters' => false,
'posts_per_page' => $posts_per_page,
'sentence' => true
);
$list_posts = get_posts($list_post_args);
// pagination
$custom_query = new WP_Query( $list_post_args );
$found_posts = $custom_query->found_posts;
$number_of_pages = ceil($found_posts / $posts_per_page);
$big = 999999999; // need an unlikely integer
$pag_arg = array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'prev_text' => __('«'),
'next_text' => __('»'),
'current' => max( 1, get_query_var('paged') ),
'total' => $number_of_pages
);
// for pagination
set_query_var('pag_arg', $pag_arg);
set_query_var('number_of_pages', $number_of_pages);
?>
<div class="all-posts">
<section>
<div class="container">
<div class="row list-of-post justify-content-center">
<div class="col-12 col-xl-10">
<div class="container">
<div class="row justify-content-center">
<div class="col-12">
<h2 class="search-title">
<span class="default_query">Kết quả tìm kiếm cho</span> <span class="keyword_quey">"<?php echo get_search_query(); ?>"</span>
</h2>
</div>
<div class="col-12">
<div class="result-list">
<?php /* Start the Loop */ ?>
<?php if ( $list_posts ) : ?>
<?php foreach($list_posts as $post) :
$post_id = $post->ID;
?>
<div class="item">
<div class="content">
<div class="meta">
<?php
$first_cat = get_the_category($post_id);
$rand_number = rand(0,count($first_cat)-1);
$cat_id = $first_cat[$rand_number]->term_id;
?>
<a class="cat-title-<?php echo $cat_id; ?>" href="<?php echo esc_url( get_category_link($cat_id) ) ?>">
<?php echo $first_cat[$rand_number]->name; ?>
</a>
<span class="dot">●</span>
<span>
<?php
date_default_timezone_set('Asia/Ho_Chi_Minh
');
$from = strtotime($post->post_date);
$today = time();
$difference = floor(($today - $from)/86400); // day
if ((get_field('update',$post_id)==true) & $difference < 7):
echo 'Mới cập nhật';
elseif ($difference == 0):
echo 'Vừa mới đăng';
elseif ($difference < 7):
echo $difference.' ngày trước';
else:
echo date('d-m-y', strtotime($post->post_date));
endif;
?>
</span>
</div>
<a class="title-excerpt" href="<?php the_permalink(); ?>" >
<h2 class="title">
<?php echo get_the_title($post_id); ?>
</h2>
<div class="excerpt">
<?php if (has_excerpt($post_id)){
echo esc_attr(get_the_excerpt($post_id));
}else{
setup_postdata( $post );
echo get_the_excerpt();
} ?>
</div>
</a>
</div>
<a href="<?php the_permalink(); ?>" >
<div class="photo">
<?php
if ( has_post_thumbnail($post_id) ) {
$postThumbnail = get_the_post_thumbnail($post_id,'medium' );
echo $postThumbnail;
}else{
$first_cat = get_the_category($post_id);
$postThumbnail = get_field('default_posts_feature_image',$first_cat[$rand_number]);
echo wp_get_attachment_image( $postThumbnail['id'],'medium');
}
?>
</div>
</a>
</div> <!-- /div item -->
<?php endforeach; ?>
<?php else: ?>
<p>Rất tiếc, không tìm thấy kết quả bạn cần tìm.</p>
<?php endif; ?>
</div>
</div>
</div> <!-- /div row small -->
</div>
</div>
</div>
</div> <!-- /div container big -->
</section>
<?php get_template_part( 'parts/pagination' ); ?>
</div><!-- /div.all-post -->
<?php get_footer(); ?>