Files
gongxue-wp-theme/archive.php
2026-06-25 15:57:22 +08:00

100 lines
6.8 KiB
PHP

<?php get_header();
$title = '知识库';
$desc = '天津专升本百科全书';
$active_cat = is_category() ? get_queried_object_id() : 0;
$active_tag = is_tag() ? get_queried_object_id() : 0;
$categories = array_filter(array_map('get_category_by_slug', array('exam-policy', 'school-intro', 'major-guide', 'study-method', 'experience')));
$tag_terms = get_terms(array(
'taxonomy' => 'post_tag',
'hide_empty' => true,
'orderby' => 'count',
'order' => 'DESC',
'number' => 8,
));
if (is_category()) {
$term = get_queried_object();
$title = $term->name;
$desc = term_description($term);
} elseif (is_tag()) {
$term = get_queried_object();
$title = '#' . $term->name;
$desc = term_description($term);
} elseif (is_search()) {
$title = sprintf(__('搜索结果: %s', 'gongxue'), get_search_query());
}
?>
<div class="pt-24 min-h-screen bg-apple-grey animate-fade-in">
<div class="max-w-5xl mx-auto px-4 md:px-6 pb-20">
<div class="flex items-center gap-4 mb-10">
<div class="p-3 bg-apple-blue rounded-xl text-white">
<svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M4 19V5"/><path d="M9 19V5"/><path d="M14 19V5"/><path d="M19 19V5"/></svg>
</div>
<div class="flex-1 min-w-0">
<h1 class="text-3xl font-bold text-apple-dark"><?php echo esc_html($title); ?></h1>
<p class="text-apple-text-gray"><?php echo wp_kses_post($desc); ?></p>
</div>
<form role="search" method="get" action="<?php echo esc_url(home_url('/')); ?>" class="hidden md:flex relative w-64">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="#9CA3AF" stroke-width="2" class="absolute left-3 top-1/2 -translate-y-1/2"><circle cx="11" cy="11" r="8"/><line x1="21" y1="21" x2="16.65" y2="16.65"/></svg>
<input type="search" name="s" value="<?php echo esc_attr(get_search_query()); ?>" placeholder="搜索文章..." class="w-full pl-9 pr-4 py-2 bg-white border border-gray-200 rounded-xl text-sm focus:outline-none focus:ring-2 focus:ring-apple-blue/20 focus:border-apple-blue">
</form>
</div>
<form role="search" method="get" action="<?php echo esc_url(home_url('/')); ?>" class="md:hidden relative mb-6">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="#9CA3AF" stroke-width="2" class="absolute left-3 top-1/2 -translate-y-1/2"><circle cx="11" cy="11" r="8"/><line x1="21" y1="21" x2="16.65" y2="16.65"/></svg>
<input type="search" name="s" value="<?php echo esc_attr(get_search_query()); ?>" placeholder="搜索文章..." class="w-full pl-9 pr-4 py-2.5 bg-white border border-gray-200 rounded-xl text-sm focus:outline-none focus:ring-2 focus:ring-apple-blue/20 focus:border-apple-blue">
</form>
<div class="flex flex-wrap gap-2 mb-8">
<a href="<?php echo esc_url(gx_knowledge_url()); ?>" class="px-4 py-2 rounded-full <?php echo (!is_category() && !is_tag() && !is_search()) ? 'bg-apple-blue text-white' : 'bg-white text-gray-500 hover:text-apple-blue'; ?>">全部</a>
<?php foreach ($categories as $cat): ?>
<a href="<?php echo esc_url(get_category_link($cat->term_id)); ?>" class="px-4 py-2 rounded-full <?php echo $active_cat === $cat->term_id ? 'bg-apple-blue text-white' : 'bg-white text-gray-500 hover:text-apple-blue'; ?>"><?php echo esc_html($cat->name); ?></a>
<?php endforeach; ?>
</div>
<?php if (!empty($tag_terms) && !is_tag()): ?>
<div class="flex flex-wrap gap-2 mb-8">
<?php foreach ($tag_terms as $tag): ?>
<a href="<?php echo esc_url(get_tag_link($tag)); ?>" class="px-3 py-1 rounded-full bg-white text-xs text-gray-500 hover:text-apple-blue">#<?php echo esc_html($tag->name); ?></a>
<?php endforeach; ?>
</div>
<?php endif; ?>
<div class="grid grid-cols-1 md:grid-cols-3 gap-6">
<aside class="space-y-2">
<a href="<?php echo esc_url(gx_knowledge_url()); ?>" class="block p-4 rounded-xl <?php echo (!is_category() && !is_tag() && !is_search()) ? 'bg-white shadow-sm font-semibold text-apple-blue' : 'text-gray-500 hover:bg-white'; ?>">全部</a>
<?php foreach ($categories as $cat): ?>
<a href="<?php echo esc_url(get_category_link($cat->term_id)); ?>" class="block p-4 rounded-xl <?php echo $active_cat === $cat->term_id ? 'bg-white shadow-sm font-semibold text-apple-blue' : 'text-gray-500 hover:bg-white'; ?>"><?php echo esc_html($cat->name); ?></a>
<?php endforeach; ?>
</aside>
<div class="md:col-span-2 space-y-4">
<?php if (have_posts()): while (have_posts()): the_post(); $cats2 = get_the_category(); $cat_name = !empty($cats2) ? $cats2[0]->name : '文章'; $tags = get_the_tags(); ?>
<article class="bg-white p-4 md:p-6 rounded-xl md:rounded-2xl shadow-sm hover:shadow-md transition-all group">
<div class="flex gap-4">
<?php if (has_post_thumbnail()): ?><div class="hidden sm:block w-28 h-20 rounded-lg overflow-hidden flex-shrink-0"><?php the_post_thumbnail('medium', array('class' => 'w-full h-full object-cover','loading'=>'lazy')); ?></div><?php endif; ?>
<div class="flex-1 min-w-0">
<h3 class="font-bold text-base md:text-lg text-apple-dark group-hover:text-apple-blue line-clamp-2">
<?php if (is_sticky(get_the_ID())): ?><span class="inline-block bg-orange-100 text-orange-600 text-[10px] px-1.5 py-0.5 rounded mr-2 align-middle">置顶</span><?php endif; ?>
<a href="<?php the_permalink(); ?>" class="hover:text-apple-blue"><?php the_title(); ?></a>
</h3>
<p class="text-sm text-gray-500 line-clamp-2 hidden md:block mt-2"><?php echo esc_html(wp_trim_words(get_the_excerpt(), 34)); ?></p>
<div class="flex flex-wrap items-center gap-2 mt-3 text-xs text-gray-400">
<span><?php echo esc_html($cat_name); ?></span>
<span><?php echo esc_html(gx_format_views(gx_get_views())); ?> 阅读</span>
<span class="hidden sm:inline"><?php echo esc_html(get_the_date('Y年n月j日')); ?></span>
<span class="flex flex-wrap gap-2">
<?php if ($tags): foreach (array_slice($tags, 0, 3) as $tag): ?><a href="<?php echo esc_url(get_tag_link($tag)); ?>" class="text-gray-400 hover:text-apple-blue">#<?php echo esc_html($tag->name); ?></a><?php endforeach; endif; ?>
</span>
</div>
</div>
</div>
</article>
<?php endwhile; gx_pagination(); else: ?>
<div class="flex flex-col items-center justify-center py-20 text-gray-400">暂无内容</div>
<?php endif; ?>
</div>
</div>
</div>
</div>
<?php get_footer(); ?>