You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

205 lines
9.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
/**
* 文章内容页
*/
?>
<?php get_header(); ?>
<!-- 新闻搜索功能模块 -->
<div class="news-search-section">
<div class="search-container">
<div class="search-header">
<h2 class="search-title">新闻资讯</h2>
</div>
<div class="search-form-wrapper">
<?php
$form_action = home_url('/');
?>
<form class="news-search-form" method="get" action="<?php echo esc_url($form_action); ?>">
<div class="search-controls">
<!-- 分类选择下拉菜单 -->
<div class="category-selector">
<?php
$current_cat = isset($_GET['category']) ? $_GET['category'] : '';
if (empty($current_cat) && is_category()) {
$current_cat = get_queried_object()->slug;
}
?>
<select name="category" id="news-category" class="category-dropdown">
<option value="" data-url="<?php echo esc_url(home_url('/category/news-center/')); ?>" <?php selected(empty($current_cat) || $current_cat === 'news-center', true); ?>>显示全部</option>
<?php
$categories = get_categories(array(
'hide_empty' => false,
'orderby' => 'name',
'order' => 'ASC'
));
foreach ($categories as $category) {
if ($category->slug === 'news-center') continue;
$cat_link = get_category_link($category->term_id);
echo '<option value="' . esc_attr($category->slug) . '" data-url="' . esc_url($cat_link) . '" ' . selected($current_cat, $category->slug, false) . '>' . esc_html($category->name) . '</option>';
}
?>
</select>
</div>
<!-- 搜索输入框 -->
<div class="search-input-wrapper">
<input type="text" name="s" id="news-search" class="search-input"
placeholder="关键词"
value="<?php echo get_search_query(); ?>"
autocomplete="off">
<div class="search-suggestions" id="search-suggestions"></div>
</div>
<!-- 搜索按钮 -->
<button type="submit" class="search-button">
<svg width="20" height="20" viewBox="0 0 20 20" fill="none">
<path d="M19 19L13 13M15 8C15 11.866 11.866 15 8 15C4.134 15 1 11.866 1 8C1 4.134 4.134 1 8 1C11.866 1 15 4.134 15 8Z" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
</svg>
</button>
</div>
<input type="hidden" name="post_type" value="post">
</form>
</div>
</div>
</div>
<div class="single-page-container">
<!-- 左侧文章列表侧边栏 -->
<aside class="sidebar-left" role="complementary" aria-label="相关文章">
<header class="sidebar-header">
<nav aria-label="返回导航">
<a href="https://cn.nenghui.com/news-center/"
class="back-link"
aria-label="返回新闻中心">
<svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true">
<path d="M10 12L6 8L10 4" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
</svg>
<span>返回新闻</span>
</a>
</nav>
</header>
<section class="recent-articles" aria-label="最新文章">
<h2 class="screen-reader-text">最新文章列表</h2>
<?php
// 优化查询参数
$recent_posts = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => 6,
'post_status' => 'publish',
'post__not_in' => array(get_the_ID()),
'no_found_rows' => true, // 性能优化:不计算总数
'update_post_meta_cache' => false, // 性能优化不缓存meta
'update_post_term_cache' => false, // 性能优化不缓存terms
'fields' => 'ids' // 只获取ID减少内存使用
));
if ($recent_posts->have_posts()) :
// 重新获取完整文章数据(只针对需要的字段)
$post_ids = $recent_posts->posts;
$posts_data = get_posts(array(
'include' => $post_ids,
'post_type' => 'post',
'post_status' => 'publish',
'orderby' => 'post__in'
));
foreach ($posts_data as $post) :
setup_postdata($post);
?>
<article class="article-item">
<div class="article-content">
<h3 class="article-title">
<a href="<?php echo esc_url(get_permalink($post->ID)); ?>"
aria-label="阅读文章:<?php echo esc_attr(get_the_title($post->ID)); ?>">
<?php echo get_the_title($post->ID); ?>
</a>
</h3>
<time class="article-date" datetime="<?php echo get_the_date('c', $post->ID); ?>">
<?php echo get_the_date('Y-m-d', $post->ID); ?>
</time>
</div>
</article>
<?php
endforeach;
wp_reset_postdata();
else :
?>
<p class="no-articles">暂无相关文章</p>
<?php endif; ?>
</section>
</aside>
<!-- 右侧主要内容区域 -->
<main class="main-content" role="main">
<article class="article-content">
<header class="article-header">
<h1 class="article-title"><?php the_title(); ?></h1>
<div class="article-meta">
<time class="article-time" datetime="<?php echo get_the_date('c'); ?>">
<span class="screen-reader-text">发布时间:</span>
<?php the_time('Y-m-d H:i:s'); ?>
</time>
<?php if (get_the_category()) : ?>
<div class="article-categories">
<span class="screen-reader-text">分类:</span>
<?php the_category(', '); ?>
</div>
<?php endif; ?>
</div>
</header>
<div class="article-body">
<?php the_content(); ?>
</div>
<?php if (get_the_tags()) : ?>
<footer class="article-footer">
<div class="article-tags">
<span class="tags-label">标签:</span>
<?php the_tags('', ', ', ''); ?>
</div>
</footer>
<?php endif; ?>
</article>
</main>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
var categorySelect = document.getElementById('news-category');
var searchForm = document.querySelector('.news-search-form');
var searchInput = document.getElementById('news-search');
if (categorySelect && searchForm) {
// 处理表单提交
searchForm.addEventListener('submit', function(e) {
var keyword = searchInput ? searchInput.value.trim() : '';
var selectedOption = categorySelect.options[categorySelect.selectedIndex];
// 如果没有关键词,但选择了分类,则跳转到分类页
if (!keyword && selectedOption && selectedOption.dataset.url) {
e.preventDefault();
window.location.href = selectedOption.dataset.url;
}
// 如果有关键词正常提交WordPress会自动处理到search.php
});
// 下拉框改变时
categorySelect.addEventListener('change', function() {
var keyword = searchInput ? searchInput.value.trim() : '';
var selectedOption = this.options[this.selectedIndex];
// 如果没有关键词,直接跳转到分类页
if (!keyword && selectedOption && selectedOption.dataset.url) {
window.location.href = selectedOption.dataset.url;
} else {
// 如果有关键词或者没有分类url比如"显示全部"且没配置url则提交表单
searchForm.submit();
}
});
}
});
</script>
<?php get_footer(); ?>