Implementing Custom Archive Pages in WordPress Themes

Posted on 16th June 2023

One of the most important aspects of theme development is creating custom archive pages. WordPress provides a number of built-in archive page templates that themes can use, but many times these pages are not sufficient for the needs of a particular theme. In this case, it is necessary to create a custom archive page template.

Creating a Custom Archive Page Template

Creating a custom archive page template is a relatively simple process. First, create a new file in your theme’s directory and name it archive.php. Next, add the following code to the top of the file:

This code tells WordPress that this file is a custom archive page template. The first line is a comment that is required by WordPress. The second line is the name of the template, which can be anything you want. The third and fourth lines are the standard header and footer code used by WordPress themes.

Adding Code to the Custom Archive Page Template

The code you add to the custom archive page template will vary depending on the needs of your theme. However, there are a few pieces of code that are often used on archive pages.

The first is the wp_query object. This object allows you to modify the main query that WordPress uses to display posts on a page. For example, the following code would limit the archive page to displaying only posts from the “News” category:

‘3’,
);
$query = new WP_Query( $args );
?>

The second piece of code that is often used on archive pages is the have_posts() function. This function is used to check if there are any posts to display. If there are posts, the code inside the while loop will be executed. If there are no posts, the code inside the else statement will be executed.

The code inside the while loop is responsible for displaying the posts. The the_post() function must be called inside the loop to setup the global post variables. Once the post variables are setup, you can use template tags to display the content of the post.

Here is an example of a custom archive page template that uses the wp_query object and the have_posts() function:

‘3’,
);
$query = new WP_Query( $args );

if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();

the_title();
the_content();
}
} else {
echo ‘No posts found’;
}

get_footer();
?>

Conclusion

Creating custom archive pages is a common task in theme development. By understanding how to create and use custom archive page templates, you will be able to better meet the needs of your theme’s users.

If you’re a WordPress theme developer, chances are you’ve been asked to add custom archive pages to a theme. Maybe you’ve even been asked to add an “Archives” widget to the sidebar.

Adding an “Archives” widget to the sidebar is a pretty straightforward task. However, adding custom archive pages is a bit more involved.

In this article, we’ll discuss what custom archive pages are and how to add them to your WordPress themes.

What are Custom Archive Pages?

Custom archive pages are pages that display a list of posts that are grouped together by a certain criteria. For example, you might have a custom archive page that displays all of the posts that have been published in the month of January.

Or, you might have a custom archive page that displays all of the posts that have been tagged with the “news” tag.

Custom archive pages are a great way to give your visitors an easy way to find all of the posts that are relevant to a certain topic.

How to Add Custom Archive Pages to Your WordPress Theme

Now that we know what custom archive pages are, let’s take a look at how to add them to your WordPress theme.

The first thing you’ll need to do is create a new file in your theme’s directory. This file will be used to display the custom archive page.

Let’s call this file “archive-news.php”.

Next, you’ll need to add the following code to the top of your new file:

This code tells WordPress that this file is going to be used to display an archive page.

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

This code does the following:

It first checks to see if there are any posts to display.

If there are posts to display, it will output the page header (which includes the archive title and description).

It will then loop through each post and output the content for each post.

Finally, it will display the posts navigation (which allows the user to navigate to older or newer posts).

If there are no posts to display, it will output the content for the “none” post type (which is typically used to display a message that no posts were found).

And that’s all there is to adding custom archive pages to your WordPress theme!

Adding an “Archives” Widget to the Sidebar

In addition to custom archive pages, you might also be asked to add an “Archives” widget to the sidebar.

Adding an “Archives” widget to the sidebar is a pretty straightforward task. However, before we dive into the code, there’s one important thing to keep in mind…

The “Archives” widget only displays a list of the most recent posts. It does not display a list of all posts.

With that said, let’s take a look at how to add an “Archives” widget to the sidebar.

First, you’ll need to add the following code to your theme’s “functions.php” file:

function my_register_widgets() {
register_widget( ‘My_Widget_Archives’ );
}
add_action( ‘widgets_init’, ‘my_register_widgets’ );

This code registers a new widget called “My_Widget_Archives”.

Next, you’ll need to create a new file in your theme’s directory. This file will be used to display the widget.

Let’s call this file “widget-archives.php”.

Next, you’ll need to add the following code to your new file:

__( ‘A widget to display the most recent posts’, ‘text_domain’ ), ) // Args
);
}

/**
* Front-end display of widget.
*
* @see WP_Widget::widget()
*
* @param array $args Widget arguments.
* @param array $instance Saved values from database.
*/
public function widget( $args, $instance ) {
$title = apply_filters( ‘widget_title’, $instance[‘title’] );

echo $args[‘before_widget’];
if ( ! empty( $title ) ) {
echo $args[‘before_title’] . $title . $args[‘after_title’];
}

// Output the most recent posts
$recent_posts = wp_get_recent_posts( array(
‘numberposts’ => 10,
‘post_status’ => ‘publish’,
) );

foreach ( $recent_posts as $post ) {
setup_postdata( $post );
?>

  • <a href="”>
  • <label for="get_field_id( ‘title’ ); ?>”>
    <input class="widefat" id="get_field_id( ‘title’ ); ?>” name=”get_field_name( ‘title’ ); ?>” type=”text” value=””>