How to Create a Custom Archive Page Template For WordPress Posts?

How to Create a Custom Archive Page Template For WordPress Posts

When working with WordPress, you’ll get to work with several page templates which includes index page (i.e. your website main page), single post template, archives page and many more. But, among all the WordPress page templates, archive page is the least used template. Sadly, many users don’t even remember about the archive template. The is simply because, the default archive page in WordPress is not at all user-friendly. However, if you’re a blogger, then you must pay attention to customizing your archive page. One best way to do so requires creating a custom page template for your archives.

Through this post, I would like to help you understand the importance of creating a custom archive page via “custom page template”. But, considering the fact that WordPress, by default, show archives added in the sidebar widgets, let’s first discuss why you need to create a custom template for your archive page anyways.

Why You Need to Create a Custom “Archive Page”?

As we had discussed previously, it is important to create a custom page template for bloggers. The reason is simple, if you’re able to draw the reader’s interests towards your blog posts, then there are higher chances that your reader will like to view your old posts. They can do so, by looking at your yearly archives provided in your website sidebar widgets.

But there is a problem: In case you’ve been blogging for more than a year, managing your archives links in your posts sidebar will become a daunting proposition. Moreover, the sidebar will look cluttered with plenty of links, making it difficult for your users to browse through your older written posts.

But, remember the only way to display the archive links on a WordPress powered site is via a widget. And so, in order to let your posts readers to dig into the archive in a clear and precise manner, you will need to create an entire sidebar dedicated to listing your archives (such as a category archive, an author archive, etc.). However, you can overcome the problem by opting for custom WordPress development that involves creating a custom page template that will list all your archives together in a single page. The best part is that a custom page template will enable you to showcase the most important content you want your visitors to read.

Creating a Custom Page Template For Archives Page

In this section, we’ll talk about the various steps involved in creating a custom page template for your WordPress blog archive page.

My Archive

Step 1 – First off, you will need to create a custom page template for your archives page. For this purpose, just open up a new file in your choice of text editor (such as Notepad++) and then save the name of your file as “page-archive.php”. Next, you’ll have to define the code at the top of your file, so that WordPress can recognize the file as a page template:

<?php
/*
Template Name: MY Archive Page Template
*/

Step 2 – Simply upload the newly created page-archive.php in the theme folder of your WordPress site. This will create a custom archives page template. Once the custom template has been created, you need to make sure that the style of the page template matches your website design. For this, simply copy your page.php file content (present in your theme’s folder) and paste it in your newly created page-archive.php.

Step 3 – After creating a custom page template, you will have to create a new  page in your WordPress website for your custom archives page. In order to do so, open up your WordPress site admin panel dashboard, and from the navigation menu browse to Pages → Add New.

You can name the page as “MY archives” or change the name to anything you like. Next,  on the right hand side of your page edit screen, you’ll be able to view a “Page Attributes” meta box. In that meta box, you’ll find an option called “Template” with a drop down menu. Click on the menu and you can view your custom page template “MY Archive Page Template” (as shown in the screen shot below), click on it. Finally, save and update the page.

In case you’re having any difficulty in creating a page template on your own, it is recommended that you should hire WordPress developer to seek assistance in getting the job done efficiently, within a pre-determined time frame.

Step 3 – Following the aforementioned steps will help create a page that will utilize your custom archives page template. But, remember that the page does not contain any content. In order to display the content (i.e. list your archive content) in your custom page template, you will need to write some code.

For example, below mentioned code snippet will list the previously published 12 posts of your WordPress website:

<?php
/*
Template Name: MY Archive Page Template
*/
get_header();
?>
<?php
$loop = new wp_Query(‘post_type=post&posts_per_page=12&offset=-12’);

while($loop->have_posts() ) : $loop->the_post();?>
<article id=”post-<?php the_ID(); ?>” <?php post_class(); ?>>

<header class=”entry-header”>
<h1 class=”entry-title”><?php the_title(); ?></h1>
</header><!– .entry-header –>

<div class=”entry-content”>
<?php the_content(); ?>

<!– This is the Fun Part –>

</div><!– .entry-content –>

</article><!– #post-## –>
<?php endwhile; // end of the loop. ?>
<?php get_footer(); ?>

Wrapping Up!

Creating a comprehensive custom archives page template will help make it easy for your blog users to navigate throughout your old content in a smooth and easy manner. Hope that this post will make you understand the need and the process of creating a custom page template for your WordPress blog archives page.

In the post, I’ve covered a simple example where your older blog posts will be displayed in the custom archive page. However, apart from displaying the list of blog entries, you can show several other things based on your needs.

 

Written by Sophia Philips

Sophia Phillips is a WordPress developer working at WordPrax Ltd.- WordPress website development company. And she also loves writing blog providing useful information about WordPress best practices and benefits it provides.

Leave a Reply

Your email address will not be published. Required fields are marked *