9be0018a1e
增加文章详情页文章筛选方式 用户点击文章标签时自动筛选出对应标签文章
416 lines
22 KiB
PHP
416 lines
22 KiB
PHP
<?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'])) : '';
|
|
$date_from = isset($_GET['date_from']) ? sanitize_text_field(wp_unslash($_GET['date_from'])) : '';
|
|
$date_to = isset($_GET['date_to']) ? sanitize_text_field(wp_unslash($_GET['date_to'])) : '';
|
|
$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';
|
|
|
|
$date_query = array();
|
|
$date_from_valid = '';
|
|
$date_to_valid = '';
|
|
if ($date_from !== '' && preg_match('/^\d{4}-\d{2}-\d{2}$/', $date_from)) {
|
|
$date_from_valid = $date_from . ' 00:00:00';
|
|
}
|
|
if ($date_to !== '' && preg_match('/^\d{4}-\d{2}-\d{2}$/', $date_to)) {
|
|
$date_to_valid = $date_to . ' 23:59:59';
|
|
}
|
|
if ($date_from_valid !== '' || $date_to_valid !== '') {
|
|
$range = array(
|
|
'inclusive' => true,
|
|
'column' => 'post_date',
|
|
);
|
|
if ($date_from_valid !== '') {
|
|
$range['after'] = $date_from_valid;
|
|
}
|
|
if ($date_to_valid !== '') {
|
|
$range['before'] = $date_to_valid;
|
|
}
|
|
$date_query[] = $range;
|
|
}
|
|
|
|
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 (!empty($date_query)) {
|
|
$list_query_args['date_query'] = $date_query;
|
|
}
|
|
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-filter-form" method="get" action="<?php echo esc_url($archive_url); ?>">
|
|
<?php if ($keyword !== '') : ?>
|
|
<input type="hidden" name="q" value="<?php echo esc_attr($keyword); ?>">
|
|
<?php endif; ?>
|
|
<div class="news-archive-filter-fields">
|
|
<label class="news-archive-filter-field">
|
|
<span data-cn="发布时间起" data-en="From">发布时间起</span>
|
|
<input type="date" name="date_from" value="<?php echo esc_attr($date_from); ?>">
|
|
</label>
|
|
<label class="news-archive-filter-field">
|
|
<span data-cn="发布时间止" data-en="To">发布时间止</span>
|
|
<input type="date" name="date_to" value="<?php echo esc_attr($date_to); ?>">
|
|
</label>
|
|
<label class="news-archive-filter-field news-archive-filter-tag">
|
|
<span data-cn="标签" data-en="Tag">标签</span>
|
|
<select name="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>
|
|
</label>
|
|
</div>
|
|
<button type="submit" data-cn="筛选" data-en="Apply">筛选</button>
|
|
</form>
|
|
|
|
<form class="news-archive-search" method="get" action="<?php echo esc_url($archive_url); ?>">
|
|
<?php if ($date_from !== '') : ?>
|
|
<input type="hidden" name="date_from" value="<?php echo esc_attr($date_from); ?>">
|
|
<?php endif; ?>
|
|
<?php if ($date_to !== '') : ?>
|
|
<input type="hidden" name="date_to" value="<?php echo esc_attr($date_to); ?>">
|
|
<?php endif; ?>
|
|
<?php if ($tag_slug !== '') : ?>
|
|
<input type="hidden" name="tag" value="<?php echo esc_attr($tag_slug); ?>">
|
|
<?php endif; ?>
|
|
<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"
|
|
>
|
|
<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,
|
|
'date_from' => $date_from,
|
|
'date_to' => $date_to,
|
|
'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(); ?>
|
|
|