-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharchive.php
More file actions
363 lines (340 loc) · 19 KB
/
archive.php
File metadata and controls
363 lines (340 loc) · 19 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
<?php get_header(); ?>
<?php
$itstudio_archive_mode = isset($GLOBALS['itstudio_archive_mode']) ? sanitize_key((string) $GLOBALS['itstudio_archive_mode']) : '';
$is_itstudio_news_page = is_post_type_archive('news') || ($itstudio_archive_mode === 'news');
$is_itstudio_notice_page = is_post_type_archive('announcement') || ($itstudio_archive_mode === 'announcement');
$is_itstudio_content_page = $is_itstudio_news_page || $is_itstudio_notice_page;
?>
<?php if ($is_itstudio_content_page) : ?>
<?php
$keyword = isset($_GET['q']) ? sanitize_text_field(wp_unslash($_GET['q'])) : '';
$tag_slug = isset($_GET['tag']) ? sanitize_title(wp_unslash($_GET['tag'])) : '';
$paged = max(1, (int) get_query_var('paged'), (int) get_query_var('page'));
$active_post_type = $is_itstudio_news_page ? 'news' : 'announcement';
$archive_url = $is_itstudio_news_page ? get_post_type_archive_link('news') : get_post_type_archive_link('announcement');
if (!$archive_url) {
$archive_url = $is_itstudio_news_page ? home_url('/news/') : home_url('/announcement/');
}
$default_cover_url = get_template_directory_uri() . '/resources/it_logo_2024.svg';
$weight_meta_key = 'itstudio_weight';
$title_cn = $is_itstudio_news_page ? '社团新闻' : '公告通知';
$title_en = $is_itstudio_news_page ? 'Club News' : 'Announcements';
$empty_cn = $is_itstudio_news_page ? '暂无新闻' : '暂无公告';
$empty_en = $is_itstudio_news_page ? 'No news found.' : 'No announcements found.';
$side_title_cn = $is_itstudio_news_page ? '要闻' : '重要公告';
$side_title_en = $is_itstudio_news_page ? 'Top Stories' : 'Important Announcements';
if ($tag_slug !== '') {
$resolved_tag = get_term_by('slug', $tag_slug, 'post_tag');
if (!($resolved_tag instanceof WP_Term)) {
$tag_slug = '';
}
}
$available_tags = get_terms(array(
'taxonomy' => 'post_tag',
'hide_empty' => true,
'orderby' => 'name',
'order' => 'ASC',
));
if (is_wp_error($available_tags)) {
$available_tags = array();
}
$list_query_args = array(
'post_type' => $active_post_type,
'post_status' => 'publish',
'posts_per_page' => 10,
'paged' => $paged,
'ignore_sticky_posts' => true,
'orderby' => 'date',
'order' => 'DESC',
'suppress_filters' => false,
);
if ($keyword !== '') {
$list_query_args['itstudio_archive_extend_search'] = true;
$list_query_args['itstudio_archive_keyword'] = $keyword;
}
if ($tag_slug !== '') {
$list_query_args['tax_query'] = array(
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => array($tag_slug),
),
);
}
$list_query = new WP_Query($list_query_args);
$featured_query = new WP_Query(array(
'post_type' => $active_post_type,
'post_status' => 'publish',
'posts_per_page' => 4,
'ignore_sticky_posts' => true,
'meta_key' => $weight_meta_key,
'orderby' => array(
'meta_value_num' => 'DESC',
'date' => 'DESC',
),
'order' => 'DESC',
'no_found_rows' => true,
));
$featured_ids = array_map('intval', wp_list_pluck($featured_query->posts, 'ID'));
wp_reset_postdata();
if (count($featured_ids) < 4) {
$fill_query = new WP_Query(array(
'post_type' => $active_post_type,
'post_status' => 'publish',
'posts_per_page' => 4 - count($featured_ids),
'ignore_sticky_posts' => true,
'post__not_in' => $featured_ids,
'orderby' => 'date',
'order' => 'DESC',
'no_found_rows' => true,
));
$featured_ids = array_merge($featured_ids, array_map('intval', wp_list_pluck($fill_query->posts, 'ID')));
wp_reset_postdata();
}
$featured_ids = array_slice(array_values(array_unique($featured_ids)), 0, 4);
?>
<main class="site-main news-archive-page">
<div class="container">
<header class="news-archive-head">
<h1 class="news-archive-title" data-cn="<?php echo esc_attr($title_cn); ?>" data-en="<?php echo esc_attr($title_en); ?>"><?php echo esc_html($title_en); ?></h1>
</header>
<div class="news-archive-tools">
<form class="news-archive-search" method="get" action="<?php echo esc_url($archive_url); ?>">
<div class="news-archive-search-controls">
<input
type="search"
name="q"
value="<?php echo esc_attr($keyword); ?>"
placeholder="<?php esc_attr_e('Search title or author...', 'itstudio'); ?>"
aria-label="<?php esc_attr_e('Search title or author', 'itstudio'); ?>"
data-cn-placeholder="搜索文章或发布者..."
data-en-placeholder="Search title or author..."
data-cn-aria-label="搜索文章或发布者"
data-en-aria-label="Search title or author"
>
<select
name="tag"
class="news-archive-search-tag"
aria-label="<?php esc_attr_e('Filter by tag', 'itstudio'); ?>"
data-cn-aria-label="按标签筛选"
data-en-aria-label="Filter by tag"
>
<option value="" data-cn="全部标签" data-en="All tags">全部标签</option>
<?php foreach ($available_tags as $tag_option) : ?>
<option value="<?php echo esc_attr($tag_option->slug); ?>" <?php selected($tag_slug, $tag_option->slug); ?>>
<?php echo esc_html($tag_option->name); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<button type="submit" data-cn="搜索" data-en="Search">搜索</button>
</form>
</div>
<div class="news-archive-layout">
<section class="news-main-column">
<?php if ($list_query->have_posts()) : ?>
<div class="news-stream">
<?php while ($list_query->have_posts()) : $list_query->the_post(); ?>
<?php
$post_id = get_the_ID();
$excerpt = function_exists('itstudio_get_post_excerpt_chars')
? itstudio_get_post_excerpt_chars($post_id, 200)
: wp_html_excerpt(wp_strip_all_tags(get_the_excerpt() ?: get_the_content()), 200, '...');
$views = function_exists('itstudio_get_post_views') ? (int) itstudio_get_post_views($post_id) : 0;
$char_count = function_exists('itstudio_get_post_char_count') ? (int) itstudio_get_post_char_count($post_id) : (int) strlen(wp_strip_all_tags(get_the_content()));
$tags = get_the_terms($post_id, 'post_tag');
?>
<article class="news-story">
<div class="news-story-body">
<h2 class="news-story-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<div class="news-story-meta">
<span class="news-story-meta-item">
<span data-cn="浏览量" data-en="Views">Views</span>
<strong><?php echo esc_html(number_format_i18n($views)); ?></strong>
</span>
<span class="news-story-meta-dot" aria-hidden="true">/</span>
<time class="news-story-meta-item" datetime="<?php echo esc_attr(get_the_date('c')); ?>">
<span data-cn="发表于" data-en="Published">Published</span>
<?php echo esc_html(get_the_date('Y-m-d H:i')); ?>
</time>
<span class="news-story-meta-dot" aria-hidden="true">/</span>
<span class="news-story-meta-item">
<span data-cn="发布者" data-en="Author">Author</span>
<?php echo esc_html(get_the_author()); ?>
</span>
<span class="news-story-meta-dot" aria-hidden="true">/</span>
<span class="news-story-meta-item">
<span data-cn="字数" data-en="Words">Words</span>
<strong><?php echo esc_html(number_format_i18n($char_count)); ?></strong>
</span>
</div>
<p class="news-story-excerpt"><?php echo esc_html($excerpt); ?></p>
<div class="news-story-tags">
<span class="news-story-tag-label" data-cn="标签" data-en="Tags">Tags</span>
<?php if (!empty($tags) && !is_wp_error($tags)) : ?>
<?php foreach (array_slice($tags, 0, 5) as $tag) : ?>
<?php
$tag_link = function_exists('itstudio_get_archive_tag_filter_url')
? itstudio_get_archive_tag_filter_url($tag, $active_post_type)
: '';
if ($tag_link === '') {
$tag_link = get_term_link($tag);
}
?>
<?php if (!is_wp_error($tag_link) && !empty($tag_link)) : ?>
<a class="news-story-tag" href="<?php echo esc_url($tag_link); ?>">#<?php echo esc_html($tag->name); ?></a>
<?php endif; ?>
<?php endforeach; ?>
<?php else : ?>
<span class="news-story-tag-empty" data-cn="无标签" data-en="No tags">No tags</span>
<?php endif; ?>
</div>
</div>
<a class="news-story-cover" href="<?php the_permalink(); ?>" aria-label="<?php echo esc_attr(get_the_title()); ?>">
<?php if (has_post_thumbnail()) : ?>
<?php the_post_thumbnail('medium_large', array('loading' => 'lazy')); ?>
<?php else : ?>
<img class="news-story-cover-fallback" src="<?php echo esc_url($default_cover_url); ?>" alt="<?php echo esc_attr(get_the_title()); ?>" loading="lazy">
<?php endif; ?>
</a>
</article>
<?php endwhile; ?>
</div>
<nav class="news-archive-pagination" aria-label="Pagination">
<?php
$pagination_base = esc_url_raw(add_query_arg('paged', '%#%', $archive_url));
echo wp_kses_post(
paginate_links(array(
'base' => $pagination_base,
'format' => '',
'current' => $paged,
'total' => max(1, (int) $list_query->max_num_pages),
'mid_size' => 2,
'prev_text' => '<span data-cn="上一页" data-en="Previous">Previous</span>',
'next_text' => '<span data-cn="下一页" data-en="Next">Next</span>',
'add_args' => array_filter(
array(
'q' => $keyword,
'tag' => $tag_slug,
),
static function ($value) {
return $value !== '';
}
),
))
);
?>
</nav>
<?php else : ?>
<p class="news-archive-empty" data-cn="<?php echo esc_attr($empty_cn); ?>" data-en="<?php echo esc_attr($empty_en); ?>"><?php echo esc_html($empty_en); ?></p>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
</section>
<aside class="news-side-column">
<section class="news-side-block">
<h2 class="news-side-title" data-cn="<?php echo esc_attr($side_title_cn); ?>" data-en="<?php echo esc_attr($side_title_en); ?>"><?php echo esc_html($side_title_en); ?></h2>
<ul class="news-side-list">
<?php if (!empty($featured_ids)) : ?>
<?php foreach ($featured_ids as $featured_id) : ?>
<?php
$featured_title = get_the_title($featured_id);
$featured_link = get_permalink($featured_id);
$featured_views = function_exists('itstudio_get_post_views') ? (int) itstudio_get_post_views($featured_id) : 0;
$featured_cover = get_the_post_thumbnail_url($featured_id, 'thumbnail');
?>
<li class="news-side-item">
<a class="news-side-cover" href="<?php echo esc_url($featured_link); ?>" aria-label="<?php echo esc_attr($featured_title); ?>">
<?php if (!empty($featured_cover)) : ?>
<img src="<?php echo esc_url($featured_cover); ?>" alt="<?php echo esc_attr($featured_title); ?>" loading="lazy">
<?php else : ?>
<img class="news-side-cover-fallback" src="<?php echo esc_url($default_cover_url); ?>" alt="<?php echo esc_attr($featured_title); ?>" loading="lazy">
<?php endif; ?>
</a>
<div class="news-side-body">
<h3 class="news-side-item-title">
<a href="<?php echo esc_url($featured_link); ?>"><?php echo esc_html($featured_title); ?></a>
</h3>
<div class="news-side-meta">
<time datetime="<?php echo esc_attr(get_the_date('c', $featured_id)); ?>"><?php echo esc_html(get_the_date('Y-m-d H:i', $featured_id)); ?></time>
<span class="news-side-meta-dot" aria-hidden="true">/</span>
<span>
<span data-cn="浏览" data-en="Views">Views</span>
<?php echo esc_html(number_format_i18n($featured_views)); ?>
</span>
</div>
</div>
</li>
<?php endforeach; ?>
<?php else : ?>
<li class="news-side-empty" data-cn="暂无文章" data-en="No posts found.">No posts found.</li>
<?php endif; ?>
</ul>
</section>
</aside>
</div>
</div>
</main>
<?php else : ?>
<main class="site-main archive-page">
<div class="container">
<header class="archive-header">
<?php
the_archive_title('<h1 class="archive-title">', '</h1>');
the_archive_description('<div class="archive-description">', '</div>');
?>
</header>
<?php if (have_posts()) : ?>
<div class="posts-grid">
<?php
while (have_posts()) :
the_post();
?>
<article id="post-<?php the_ID(); ?>" <?php post_class('archive-item'); ?>>
<?php if (has_post_thumbnail()) : ?>
<div class="archive-thumbnail">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('medium'); ?>
</a>
</div>
<?php endif; ?>
<div class="archive-content">
<h2 class="archive-item-title">
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</h2>
<div class="archive-meta">
<time datetime="<?php echo esc_attr(get_the_date('c')); ?>">
<?php echo esc_html(get_the_date()); ?>
</time>
<?php if (get_post_type() === 'post') : ?>
<span class="separator">|</span>
<span class="author"><?php the_author(); ?></span>
<?php endif; ?>
</div>
<?php if (has_excerpt()) : ?>
<div class="archive-excerpt">
<?php the_excerpt(); ?>
</div>
<?php endif; ?>
<a href="<?php the_permalink(); ?>" class="read-more">
<?php esc_html_e('Read More', 'itstudio'); ?> ->
</a>
</div>
</article>
<?php endwhile; ?>
</div>
<div class="pagination">
<?php
the_posts_pagination(array(
'mid_size' => 2,
'prev_text' => __('Previous', 'itstudio'),
'next_text' => __('Next', 'itstudio'),
));
?>
</div>
<?php else : ?>
<p><?php esc_html_e('No posts found.', 'itstudio'); ?></p>
<?php endif; ?>
</div>
</main>
<?php endif; ?>
<?php get_footer(); ?>