The Archive.php Template
Build the archive.php template for category, tag, author, and date-based post listings.
The Archive.php Template
The archive.php template handles category pages, tag pages, author pages, and date-based archives. It displays a list of posts that match the current archive.
Creating archive.php
<?php get_header(); ?>
<main id="main-content">
<div class="container">
<div class="content-wrapper">
<div class="primary-content">
<header class="archive-header">
<h1 class="archive-title">
<?php
if (is_category()) {
single_cat_title('Category: ');
} elseif (is_tag()) {
single_tag_title('Tag: ');
} elseif (is_author()) {
echo 'Author: ' . get_the_author();
} elseif (is_date()) {
if (is_year()) {
echo 'Year: ' . get_the_date('Y');
} elseif (is_month()) {
echo 'Month: ' . get_the_date('F Y');
} elseif (is_day()) {
echo 'Day: ' . get_the_date('F j, Y');
}
} else {
echo 'Archives';
}
?>
</h1>
<?php if (term_description()) : ?>
<div class="archive-description">
<?php echo term_description(); ?>
</div>
<?php endif; ?>
</header>
<div class="post-grid">
<?php if (have_posts()) :
while (have_posts()) : the_post(); ?>
<article class="post-card">
<?php if (has_post_thumbnail()) : ?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('medium_large'); ?>
</a>
<?php endif; ?>
<div class="post-card-body">
<h3><a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a></h3>
<p class="post-meta">
<span class="date"><?php echo get_the_date(); ?></span>
<span class="author">by <?php the_author(); ?></span>
</p>
<?php the_excerpt(); ?>
<a href="<?php the_permalink(); ?>" class="read-more">Read More →</a>
</div>
</article>
<?php endwhile; ?>
</div>
<!-- Pagination -->
<div class="pagination">
<?php the_posts_pagination(array(
'mid_size' => 2,
'prev_text' => '« Previous',
'next_text' => 'Next »',
)); ?>
</div>
<?php else : ?>
<p>No posts found in this archive.</p>
<?php endif; ?>
</div>
<?php get_sidebar(); ?>
</div>
</div>
</main>
<?php get_footer(); ?>
Conditional Tags Used
| Tag | Returns True When |
|---|---|
is_category() | Viewing a category archive |
is_tag() | Viewing a tag archive |
is_author() | Viewing an author's posts |
is_date() | Viewing a date-based archive |
is_year() | Viewing a yearly archive |
is_month() | Viewing a monthly archive |
Pagination
the_posts_pagination() outputs page navigation when there are more posts than fit on one page (controlled by Settings → Reading → "Blog pages show at most").
.pagination {
text-align: center;
margin-top: 40px;
padding-top: 20px;
border-top: 1px solid #eee;
}
.pagination .page-numbers {
display: inline-block;
padding: 8px 14px;
margin: 0 3px;
background: #f0f4f8;
color: #555;
border-radius: 4px;
font-size: 14px;
}
.pagination .page-numbers.current {
background: #3498db;
color: #fff;
}
.pagination .page-numbers:hover:not(.current) {
background: #ddd;
}
Key Takeaways
archive.phphandles category, tag, author, and date archives- Use conditional tags to display the appropriate archive title
term_description()outputs the category/tag description if one existsthe_posts_pagination()adds page navigation for large archives- The Loop automatically shows the posts relevant to the current archive