站上还有一类页面:点「2026 年 11 月」看这个月的文章,点作者名看这个人写的所有文章。这些统称归档页。
跟分类页一样,它也需要模板。区别在于——一个文件能管四种归档。
分类归档也会落到它头上,但如果你写了 category.php,分类页会优先用那个。顺序是:越具体的模板越优先。
<?php get_header(); ?>
<h1><?php the_archive_title(); ?></h1>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php the_excerpt(); ?>
<?php endwhile; ?>
<?php endif; ?>
<?php get_footer(); ?>
跟 category.php 几乎一样,只有标题那行不同——因为归档页的标题可能是「2026年11月」,也可能是「作者:张三」,你没法提前写死。
the_archive_title() 会自动输出合适的前缀:日期归档输出年月,作者归档输出作者名。它是 WordPress 4.1 之后才有的函数。
在那之前,大家要写一长串 if ( is_category() ) ... elseif ( is_author() ) ...。现在一行顶一堆,遇到旧教程里那一长串,知道是历史写法就行。
现在归档页能用了。下一篇:搜索。
本系列:迭代式建主题 · 目录(72 篇) | 上一篇 (06) | 下一篇 (08)