I’ll build this as a complete advanced
archive.php template for your RX theme, with safe WordPress functions, SEO-friendly archive headers, breadcrumbs, post cards, pagination, schema, and fallback handling.My dear friend, replace your RX theme archive.php with this advanced version. WordPress uses archive.php as the generic archive template when a more specific file such as category.php, tag.php, author.php, or date.php is not available. This code uses WordPress archive title and archive description functions, plus safe pagination with the_posts_pagination().
<?php
/**
* RX Theme Advanced Archive Template
*
* File: archive.php
*
* Features:
* - Category, tag, taxonomy, author, date, post type archive support
* - SEO-friendly archive header
* - Breadcrumb navigation
* - Archive description
* - Archive statistics
* - Post grid/list layout
* - Featured image support
* - Reading time
* - Categories, tags, comments, author, date
* - Author archive bio box
* - Child category/term links
* - Numeric pagination
* - Accessible markup
* - JSON-LD CollectionPage schema
* - Sidebar support
* - Empty archive fallback
*
* @package RX_Theme
*/
defined( 'ABSPATH' ) || exit;
get_header();
/**
* Get archive title without unsafe HTML.
*/
if ( ! function_exists( 'rx_archive_title' ) ) {
function rx_archive_title() {
$title = get_the_archive_title();
if ( is_category() || is_tag() || is_tax() ) {
$obj = get_queried_object();
if ( ! empty( $obj->name ) ) {
$title = $obj->name;
}
}
if ( is_author() ) {
$title = get_the_author_meta( 'display_name', get_queried_object_id() );
}
if ( is_post_type_archive() ) {
$title = post_type_archive_title( '', false );
}
return wp_strip_all_tags( $title );
}
}
/**
* Get smart archive subtitle.
*/
if ( ! function_exists( 'rx_archive_subtitle' ) ) {
function rx_archive_subtitle() {
global $wp_query;
$count = isset( $wp_query->found_posts ) ? absint( $wp_query->found_posts ) : 0;
if ( is_category() ) {
return sprintf(
/* translators: %s: number of posts */
_n( '%s article found in this category.', '%s articles found in this category.', $count, 'rx-theme' ),
number_format_i18n( $count )
);
}
if ( is_tag() ) {
return sprintf(
_n( '%s article found for this tag.', '%s articles found for this tag.', $count, 'rx-theme' ),
number_format_i18n( $count )
);
}
if ( is_tax() ) {
return sprintf(
_n( '%s article found in this archive.', '%s articles found in this archive.', $count, 'rx-theme' ),
number_format_i18n( $count )
);
}
if ( is_author() ) {
return sprintf(
_n( '%s article written by this author.', '%s articles written by this author.', $count, 'rx-theme' ),
number_format_i18n( $count )
);
}
if ( is_day() ) {
return esc_html__( 'Daily archive from RX Theme.', 'rx-theme' );
}
if ( is_month() ) {
return esc_html__( 'Monthly archive from RX Theme.', 'rx-theme' );
}
if ( is_year() ) {
return esc_html__( 'Yearly archive from RX Theme.', 'rx-theme' );
}
if ( is_post_type_archive() ) {
return sprintf(
_n( '%s item found in this post type archive.', '%s items found in this post type archive.', $count, 'rx-theme' ),
number_format_i18n( $count )
);
}
return sprintf(
_n( '%s article found.', '%s articles found.', $count, 'rx-theme' ),
number_format_i18n( $count )
);
}
}
/**
* Estimate post reading time.
*/
if ( ! function_exists( 'rx_archive_reading_time' ) ) {
function rx_archive_reading_time( $post_id = 0 ) {
$post_id = $post_id ? absint( $post_id ) : get_the_ID();
$text = wp_strip_all_tags( get_post_field( 'post_content', $post_id ) );
$words = str_word_count( $text );
$minutes = max( 1, ceil( $words / 200 ) );
return sprintf(
_n( '%s min read', '%s min read', $minutes, 'rx-theme' ),
number_format_i18n( $minutes )
);
}
}
/**
* Simple breadcrumb.
*/
if ( ! function_exists( 'rx_archive_breadcrumbs' ) ) {
function rx_archive_breadcrumbs() {
?>
<nav class="rx-breadcrumbs" aria-label="<?php esc_attr_e( 'Breadcrumb', 'rx-theme' ); ?>">
<ol class="rx-breadcrumbs__list">
<li class="rx-breadcrumbs__item">
<a href="<?php echo esc_url( home_url( '/' ) ); ?>">
<?php esc_html_e( 'Home', 'rx-theme' ); ?>
</a>
</li>
<li class="rx-breadcrumbs__item" aria-current="page">
<?php echo esc_html( rx_archive_title() ); ?>
</li>
</ol>
</nav>
<?php
}
}
/**
* Render child terms for category/taxonomy archives.
*/
if ( ! function_exists( 'rx_archive_child_terms' ) ) {
function rx_archive_child_terms() {
if ( ! is_category() && ! is_tax() ) {
return;
}
$queried = get_queried_object();
if ( empty( $queried->term_id ) || empty( $queried->taxonomy ) ) {
return;
}
$children = get_terms(
array(
'taxonomy' => $queried->taxonomy,
'parent' => absint( $queried->term_id ),
'hide_empty' => true,
'number' => 12,
)
);
if ( empty( $children ) || is_wp_error( $children ) ) {
return;
}
?>
<section class="rx-archive-terms" aria-labelledby="rx-child-terms-title">
<h2 id="rx-child-terms-title" class="rx-section-title">
<?php esc_html_e( 'Explore More Topics', 'rx-theme' ); ?>
</h2>
<div class="rx-term-cloud">
<?php foreach ( $children as $child ) : ?>
<a class="rx-term-cloud__link" href="<?php echo esc_url( get_term_link( $child ) ); ?>">
<span><?php echo esc_html( $child->name ); ?></span>
<small><?php echo esc_html( number_format_i18n( $child->count ) ); ?></small>
</a>
<?php endforeach; ?>
</div>
</section>
<?php
}
}
/**
* Render author box on author archive.
*/
if ( ! function_exists( 'rx_archive_author_box' ) ) {
function rx_archive_author_box() {
if ( ! is_author() ) {
return;
}
$author_id = get_queried_object_id();
$name = get_the_author_meta( 'display_name', $author_id );
$description = get_the_author_meta( 'description', $author_id );
$website = get_the_author_meta( 'user_url', $author_id );
?>
<section class="rx-author-box" aria-labelledby="rx-author-title">
<div class="rx-author-box__avatar">
<?php echo get_avatar( $author_id, 96, '', esc_attr( $name ), array( 'class' => 'rx-author-avatar' ) ); ?>
</div>
<div class="rx-author-box__content">
<h2 id="rx-author-title" class="rx-author-box__name">
<?php echo esc_html( $name ); ?>
</h2>
<?php if ( $description ) : ?>
<p class="rx-author-box__bio">
<?php echo esc_html( $description ); ?>
</p>
<?php endif; ?>
<?php if ( $website ) : ?>
<a class="rx-author-box__website" href="<?php echo esc_url( $website ); ?>" rel="nofollow noopener">
<?php esc_html_e( 'Visit website', 'rx-theme' ); ?>
</a>
<?php endif; ?>
</div>
</section>
<?php
}
}
/**
* Render one archive post card.
*/
if ( ! function_exists( 'rx_archive_post_card' ) ) {
function rx_archive_post_card() {
$post_id = get_the_ID();
?>
<article id="post-<?php the_ID(); ?>" <?php post_class( 'rx-archive-card' ); ?>>
<?php if ( has_post_thumbnail() ) : ?>
<a class="rx-archive-card__thumb" href="<?php the_permalink(); ?>" aria-label="<?php the_title_attribute(); ?>">
<?php
the_post_thumbnail(
'large',
array(
'class' => 'rx-archive-card__image',
'loading' => 'lazy',
'alt' => the_title_attribute(
array(
'echo' => false,
)
),
)
);
?>
</a>
<?php else : ?>
<a class="rx-archive-card__thumb rx-archive-card__thumb--empty" href="<?php the_permalink(); ?>" aria-label="<?php the_title_attribute(); ?>">
<span><?php esc_html_e( 'RX', 'rx-theme' ); ?></span>
</a>
<?php endif; ?>
<div class="rx-archive-card__body">
<div class="rx-archive-card__meta rx-archive-card__meta--top">
<span class="rx-meta-item rx-meta-date">
<time datetime="<?php echo esc_attr( get_the_date( DATE_W3C ) ); ?>">
<?php echo esc_html( get_the_date() ); ?>
</time>
</span>
<span class="rx-meta-item rx-meta-reading">
<?php echo esc_html( rx_archive_reading_time( $post_id ) ); ?>
</span>
<?php if ( comments_open() || get_comments_number() ) : ?>
<span class="rx-meta-item rx-meta-comments">
<?php
printf(
esc_html( _n( '%s comment', '%s comments', get_comments_number(), 'rx-theme' ) ),
esc_html( number_format_i18n( get_comments_number() ) )
);
?>
</span>
<?php endif; ?>
</div>
<?php
$categories = get_the_category();
if ( ! empty( $categories ) ) :
?>
<div class="rx-archive-card__categories">
<?php foreach ( array_slice( $categories, 0, 3 ) as $category ) : ?>
<a href="<?php echo esc_url( get_category_link( $category ) ); ?>">
<?php echo esc_html( $category->name ); ?>
</a>
<?php endforeach; ?>
</div>
<?php endif; ?>
<header class="rx-archive-card__header">
<h2 class="rx-archive-card__title">
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</h2>
</header>
<div class="rx-archive-card__excerpt">
<?php
if ( has_excerpt() ) {
echo wp_kses_post( wpautop( get_the_excerpt() ) );
} else {
echo wp_kses_post(
wpautop(
wp_trim_words(
wp_strip_all_tags( get_the_content() ),
32,
'...'
)
)
);
}
?>
</div>
<footer class="rx-archive-card__footer">
<div class="rx-archive-card__author">
<?php echo get_avatar( get_the_author_meta( 'ID' ), 32, '', '', array( 'class' => 'rx-mini-avatar' ) ); ?>
<span>
<?php esc_html_e( 'By', 'rx-theme' ); ?>
<a href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ); ?>">
<?php echo esc_html( get_the_author() ); ?>
</a>
</span>
</div>
<a class="rx-read-more" href="<?php the_permalink(); ?>" aria-label="<?php echo esc_attr( sprintf( __( 'Read more about %s', 'rx-theme' ), get_the_title() ) ); ?>">
<?php esc_html_e( 'Read More', 'rx-theme' ); ?>
<span aria-hidden="true">→</span>
</a>
</footer>
<?php
$tags = get_the_tags();
if ( ! empty( $tags ) ) :
?>
<div class="rx-archive-card__tags">
<?php foreach ( array_slice( $tags, 0, 5 ) as $tag ) : ?>
<a href="<?php echo esc_url( get_tag_link( $tag ) ); ?>">
#<?php echo esc_html( $tag->name ); ?>
</a>
<?php endforeach; ?>
</div>
<?php endif; ?>
</div>
</article>
<?php
}
}
/**
* Archive pagination.
*/
if ( ! function_exists( 'rx_archive_pagination' ) ) {
function rx_archive_pagination() {
the_posts_pagination(
array(
'mid_size' => 2,
'prev_text' => esc_html__( '← Previous', 'rx-theme' ),
'next_text' => esc_html__( 'Next →', 'rx-theme' ),
'screen_reader_text' => esc_html__( 'Archive navigation', 'rx-theme' ),
'aria_label' => esc_attr__( 'Archive posts', 'rx-theme' ),
'class' => 'rx-pagination',
)
);
}
}
/**
* Empty archive fallback.
*/
if ( ! function_exists( 'rx_archive_no_results' ) ) {
function rx_archive_no_results() {
?>
<section class="rx-no-results">
<h2><?php esc_html_e( 'No posts found', 'rx-theme' ); ?></h2>
<p>
<?php esc_html_e( 'Sorry, no articles were found in this archive. Please try another topic or use the search box below.', 'rx-theme' ); ?>
</p>
<div class="rx-no-results__search">
<?php get_search_form(); ?>
</div>
<a class="rx-button rx-button--home" href="<?php echo esc_url( home_url( '/' ) ); ?>">
<?php esc_html_e( 'Back to Homepage', 'rx-theme' ); ?>
</a>
</section>
<?php
}
}
/**
* CollectionPage schema.
*/
if ( ! function_exists( 'rx_archive_schema' ) ) {
function rx_archive_schema() {
if ( ! is_archive() ) {
return;
}
$schema = array(
'@context' => 'https://schema.org',
'@type' => 'CollectionPage',
'name' => rx_archive_title(),
'description' => wp_strip_all_tags( get_the_archive_description() ? get_the_archive_description() : rx_archive_subtitle() ),
'url' => get_pagenum_link( get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1 ),
'isPartOf' => array(
'@type' => 'WebSite',
'name' => get_bloginfo( 'name' ),
'url' => home_url( '/' ),
),
);
echo '<script type="application/ld+json">' . wp_json_encode( $schema, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ) . '</script>' . "\n";
}
}
rx_archive_schema();
?>
<main id="primary" class="rx-site-main rx-archive-main">
<section class="rx-archive-hero">
<div class="rx-container">
<?php rx_archive_breadcrumbs(); ?>
<div class="rx-archive-hero__content">
<p class="rx-archive-kicker">
<?php esc_html_e( 'RX Archive', 'rx-theme' ); ?>
</p>
<h1 class="rx-archive-title">
<?php echo esc_html( rx_archive_title() ); ?>
</h1>
<p class="rx-archive-subtitle">
<?php echo esc_html( rx_archive_subtitle() ); ?>
</p>
<?php if ( get_the_archive_description() ) : ?>
<div class="rx-archive-description">
<?php echo wp_kses_post( wpautop( get_the_archive_description() ) ); ?>
</div>
<?php endif; ?>
</div>
</div>
</section>
<div class="rx-container rx-archive-layout">
<div class="rx-archive-content">
<?php rx_archive_author_box(); ?>
<?php rx_archive_child_terms(); ?>
<?php if ( have_posts() ) : ?>
<section class="rx-archive-posts" aria-labelledby="rx-archive-posts-title">
<h2 id="rx-archive-posts-title" class="screen-reader-text">
<?php esc_html_e( 'Archive Posts', 'rx-theme' ); ?>
</h2>
<div class="rx-archive-grid">
<?php
while ( have_posts() ) :
the_post();
rx_archive_post_card();
endwhile;
?>
</div>
</section>
<?php rx_archive_pagination(); ?>
<?php else : ?>
<?php rx_archive_no_results(); ?>
<?php endif; ?>
</div>
<?php if ( is_active_sidebar( 'sidebar-1' ) ) : ?>
<aside id="secondary" class="rx-sidebar widget-area" aria-label="<?php esc_attr_e( 'Archive Sidebar', 'rx-theme' ); ?>">
<?php dynamic_sidebar( 'sidebar-1' ); ?>
</aside>
<?php endif; ?>
</div>
</main>
<?php
get_footer();
Optional CSS for style.css
Add this to your RX theme style.css to make the archive page beautiful:
.rx-container {
max-width: 1200px;
margin: 0 auto;
padding-left: 20px;
padding-right: 20px;
}
.rx-archive-hero {
padding: 48px 0 36px;
background: #f8fafc;
border-bottom: 1px solid #e5e7eb;
}
.rx-breadcrumbs {
font-size: 14px;
margin-bottom: 20px;
}
.rx-breadcrumbs__list {
display: flex;
flex-wrap: wrap;
gap: 8px;
list-style: none;
padding: 0;
margin: 0;
}
.rx-breadcrumbs__item::after {
content: "/";
margin-left: 8px;
color: #9ca3af;
}
.rx-breadcrumbs__item:last-child::after {
content: "";
margin: 0;
}
.rx-archive-kicker {
text-transform: uppercase;
letter-spacing: 0.08em;
font-weight: 700;
font-size: 13px;
margin: 0 0 8px;
color: #2563eb;
}
.rx-archive-title {
font-size: clamp(32px, 5vw, 56px);
line-height: 1.05;
margin: 0 0 14px;
color: #111827;
}
.rx-archive-subtitle {
font-size: 18px;
color: #4b5563;
margin: 0 0 16px;
}
.rx-archive-description {
max-width: 780px;
color: #374151;
font-size: 17px;
line-height: 1.75;
}
.rx-archive-layout {
display: grid;
grid-template-columns: minmax(0, 1fr) 320px;
gap: 36px;
padding-top: 40px;
padding-bottom: 60px;
}
.rx-archive-grid {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 28px;
}
.rx-archive-card {
background: #ffffff;
border: 1px solid #e5e7eb;
border-radius: 18px;
overflow: hidden;
box-shadow: 0 10px 30px rgba(15, 23, 42, 0.06);
transition: transform 0.2s ease, box-shadow 0.2s ease;
}
.rx-archive-card:hover {
transform: translateY(-4px);
box-shadow: 0 18px 45px rgba(15, 23, 42, 0.12);
}
.rx-archive-card__thumb {
display: block;
aspect-ratio: 16 / 9;
background: #e5e7eb;
overflow: hidden;
text-decoration: none;
}
.rx-archive-card__image {
width: 100%;
height: 100%;
object-fit: cover;
display: block;
}
.rx-archive-card__thumb--empty {
display: flex;
align-items: center;
justify-content: center;
font-size: 42px;
font-weight: 800;
color: #94a3b8;
}
.rx-archive-card__body {
padding: 22px;
}
.rx-archive-card__meta {
display: flex;
flex-wrap: wrap;
gap: 10px;
font-size: 13px;
color: #6b7280;
margin-bottom: 12px;
}
.rx-archive-card__categories {
display: flex;
flex-wrap: wrap;
gap: 8px;
margin-bottom: 12px;
}
.rx-archive-card__categories a,
.rx-archive-card__tags a {
font-size: 13px;
text-decoration: none;
background: #eef2ff;
color: #3730a3;
padding: 5px 9px;
border-radius: 999px;
}
.rx-archive-card__title {
font-size: 24px;
line-height: 1.25;
margin: 0 0 12px;
}
.rx-archive-card__title a {
color: #111827;
text-decoration: none;
}
.rx-archive-card__title a:hover {
color: #2563eb;
}
.rx-archive-card__excerpt {
color: #4b5563;
font-size: 16px;
line-height: 1.7;
}
.rx-archive-card__footer {
display: flex;
align-items: center;
justify-content: space-between;
gap: 16px;
margin-top: 18px;
}
.rx-archive-card__author {
display: flex;
align-items: center;
gap: 8px;
font-size: 14px;
color: #4b5563;
}
.rx-mini-avatar,
.rx-author-avatar {
border-radius: 999px;
}
.rx-read-more {
font-weight: 700;
text-decoration: none;
color: #2563eb;
white-space: nowrap;
}
.rx-archive-card__tags {
display: flex;
flex-wrap: wrap;
gap: 8px;
margin-top: 18px;
}
.rx-author-box,
.rx-archive-terms,
.rx-no-results {
background: #ffffff;
border: 1px solid #e5e7eb;
border-radius: 18px;
padding: 24px;
margin-bottom: 30px;
}
.rx-author-box {
display: flex;
gap: 18px;
align-items: flex-start;
}
.rx-author-box__name {
margin: 0 0 8px;
}
.rx-author-box__bio {
margin: 0 0 10px;
color: #4b5563;
line-height: 1.7;
}
.rx-term-cloud {
display: flex;
flex-wrap: wrap;
gap: 10px;
}
.rx-term-cloud__link {
display: inline-flex;
gap: 8px;
align-items: center;
text-decoration: none;
padding: 9px 12px;
border-radius: 999px;
background: #f3f4f6;
color: #111827;
}
.rx-term-cloud__link small {
color: #6b7280;
}
.rx-pagination {
margin-top: 40px;
}
.rx-pagination .nav-links {
display: flex;
flex-wrap: wrap;
gap: 8px;
justify-content: center;
}
.rx-pagination .page-numbers {
display: inline-flex;
min-width: 42px;
height: 42px;
align-items: center;
justify-content: center;
padding: 0 12px;
border-radius: 999px;
background: #f3f4f6;
color: #111827;
text-decoration: none;
font-weight: 700;
}
.rx-pagination .page-numbers.current,
.rx-pagination .page-numbers:hover {
background: #2563eb;
color: #ffffff;
}
.rx-sidebar {
position: relative;
}
.rx-sidebar .widget {
background: #ffffff;
border: 1px solid #e5e7eb;
border-radius: 18px;
padding: 22px;
margin-bottom: 24px;
}
.screen-reader-text {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap;
border: 0;
}
@media (max-width: 960px) {
.rx-archive-layout {
grid-template-columns: 1fr;
}
.rx-archive-grid {
grid-template-columns: 1fr;
}
}
@media (max-width: 600px) {
.rx-archive-hero {
padding: 34px 0 28px;
}
.rx-archive-card__footer {
align-items: flex-start;
flex-direction: column;
}
.rx-author-box {
flex-direction: column;
}
}
Optional sidebar support in functions.php
Add this only if your RX theme does not already register sidebar-1:
function rx_theme_register_archive_sidebar() {
register_sidebar(
array(
'name' => esc_html__( 'Main Sidebar', 'rx-theme' ),
'id' => 'sidebar-1',
'description' => esc_html__( 'Widgets shown beside archive pages.', 'rx-theme' ),
'before_widget' => '<section id="%1$s" class="widget %2$s">',
'after_widget' => '</section>',
'before_title' => '<h2 class="widget-title">',
'after_title' => '</h2>',
)
);
}
add_action( 'widgets_init', 'rx_theme_register_archive_sidebar' );
Place the first big code in:
/wp-content/themes/rx-theme/archive.php
Then clear cache and test category, tag, author, and date archive pages.