Building Custom Archive Pages in WordPress Themes

Posted on 17th June 2023

When it comes to WordPress development, one of the most common tasks is creating custom archive pages. This is often necessary when working with custom post types or custom taxonomies.

In this article, we’ll discuss what archive pages are and how to create custom archive pages in WordPress themes.

What are Archive Pages?

An archive page is a page that displays a collection of posts. WordPress automatically generates archive pages for dates, categories, tags, and authors. These pages are generated based on the URL structure of WordPress.

For example, the URL for a category archive page would be something like this:

http://example.com/category/news

And the URL for a tag archive page would be something like this:

http://example.com/tag/wordpress

Similarly, the URL for an author archive page would be something like this:

http://example.com/author/john

WordPress also generates an archive page for each month and year. The URL for a monthly archive would be something like this:

http://example.com/2017/05

And the URL for a yearly archive would be something like this:

http://example.com/2017

Creating Custom Archive Pages

Now that we know what archive pages are, let’s take a look at how to create custom archive pages in WordPress.

There are two ways to create custom archive pages in WordPress:

1. Use the WordPress Template Hierarchy
2. Use the WP_Query class

We’ll discuss both methods in detail below.

Using the WordPress Template Hierarchy

The WordPress template hierarchy is a system that WordPress uses to decide which template file to load when a visitor arrives at your website.

For example, when a visitor arrives at a category archive page, WordPress will first look for a template file named category.php. If this file doesn’t exist, WordPress will look for a template file named archive.php.

The WordPress template hierarchy is helpful when creating custom archive pages because it allows you to target specific archive pages with specific template files.

For example, let’s say you want to create a custom template file for your category archive pages. You can do this by creating a file named category.php and adding the following code to it:

<?php

/*
Template Name: Category Archive
*/

// Your code goes here…

This code tells WordPress to use this template file for category archive pages.

If you want to target a specific category archive page, you can do so by using the category slug in the template file name. For example, let's say you want to create a custom template file for the category archive page with the slug "news". In this case, you would name the template file category-news.php.

Using the WP_Query Class

The WP_Query class is a powerful tool that allows you to query the WordPress database and return specific data.

This class can be used to create custom archive pages. For example, let's say you want to create an archive page that displays all of the posts in the "news" category.

To do this, you would use the WP_Query class like this:

‘news’
);

$query = new WP_Query( $args );

if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
// Your code goes here…
}
}

wp_reset_postdata();

This code will query the WordPress database and return all of the posts in the “news” category.

You can also use the WP_Query class to create an archive page for a custom post type. For example, let’s say you want to create an archive page that displays all of the posts in the “books” custom post type.

To do this, you would use the WP_Query class like this:

‘books’
);

$query = new WP_Query( $args );

if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
// Your code goes here…
}
}

wp_reset_postdata();

This code will query the WordPress database and return all of the posts in the “books” custom post type.

Conclusion

In this article, we’ve discussed what archive pages are and how to create custom archive pages in WordPress. We’ve also looked at two methods for creating custom archive pages: using the WordPress template hierarchy and using the WP_Query class.

Building Custom Archive Pages in WordPress Themes

In this article, we’ll show you how to build custom archive pages in WordPress themes.

First, you’ll need to create a new file in your theme called archive.php.

Next, you’ll need to add the following code to your archive.php file:

This code will output the archive page title and a list of the latest posts.

If you want to output a specific number of posts on your archive page, you can use the WP_Query class.

For example, the following code will output the latest 5 posts:

Archives

5

);

$query = new WP_Query( $args );

if ( $query->have_posts() ) :

while ( $query->have_posts() ) :

$query->the_post();

?>

You can learn more about the WP_Query class in the WordPress Codex.

If you want to output a list of posts from a specific category, you can use the following code:

Archives

3

);

$query = new WP_Query( $args );

if ( $query->have_posts() ) :

while ( $query->have_posts() ) :

$query->the_post();

?>

This code will output a list of posts from the category with the ID of 3.

You can learn more about the WP_Query class in the WordPress Codex.

If you want to output a list of posts from a specific tag, you can use the following code:

Archives

‘wordpress’

);

$query = new WP_Query( $args );

if ( $query->have_posts() ) :

while ( $query->have_posts() ) :

$query->the_post();

?>

This code will output a list of posts tagged with “wordpress”.

You can learn more about the WP_Query class in the WordPress Codex.

If you want to output a list of posts from a specific author, you can use the following code:

Archives

1

);

$query = new WP_Query( $args );

if ( $query->have_posts() ) :

while ( $query->have_posts() ) :

$query->the_post();

?>

This code will output a list of posts by the author with the ID of 1.

You can learn more about the WP_Query class in the WordPress Codex.

If you want to output a list of posts from a specific date, you can use the following code:

Archives

array(

array(

‘year’ => 2015,

‘month’ => 12,

‘day’ => 25,

),

),

);

$query = new WP_Query( $args );

if ( $query->have_posts() ) :

while ( $query->have_posts() ) :

$query->the_post();

?>

This code will output a list of posts from December 25th, 2015.

You can learn more about the WP_Query class in the WordPress Codex.

You can also output a list of posts from a specific date range. For example, the following code will output a list of posts from January 1st to January 31st:

Archives

array(

array(

‘after’ => array(

‘year’ => 2014,

‘month’ => 12,

‘day’ => 31,

),

‘before’ => array(

‘year’ => 2015,

‘month’ => 1,

‘day’ => 31,

),

‘inclusive’ => true,

),

),

);

$query = new WP_Query( $args );

if ( $query->have_posts() ) :

while ( $query->have_posts() ) :

$query->the_post();

?>

This code will output a list of posts from January 1st to January 31st, 2015.

You can learn more about the WP_Query class in the WordPress Codex.

If you want to output a list of posts from a specific month, you can use the following code:

Archives

array(

array(

‘year’ => 2015,

‘month’ => 12,

),

),

);

$query = new WP_Query( $args );

if ( $query->have_posts() ) :

while ( $query->have_posts() ) :

$query->the_post();

?>

<?php

// The footer