Create A WordPress Archives Template for Your Theme

An archives page is great way of linking to all of your important archives such as monthly archives and category archives on one dedicated page. It's a great alternative to simply linking to your older posts in your sidebar and footer. Surprisingly, a lot of themes out there don't include one by default... so today, we're going to show you a quick way of building one from scratch.

The Kubrick WordPress Theme, which was the default theme of WordPress for 5 years (version 1.5 until 3.0), came packaged with its own archives.php template. Sadly, the last two default WordPress themes (TwentyTen and TwentyEleven) have not.

As most WordPress themes do not come with an Archives template, it is frequently necessary to create your own. In this tutorial we will be walking you through how you can create a simple archives page using the current default theme TwentyEleven however these steps can be applied to any WordPress design.


Before We Start

Please do not confuse the archive.php template and the archives.php template. The archive.php template is included in most themes and is used to generate monthly archives, category archives, author pages etc (the parent file index.php is used if no archive.php template is available). For example, archives like www.yoursite.com/2011/12/ and www.yoursite.com/category/main-category/.

What we are creating today is the archives.php template. This helps create a dedicated Archive page on your website that lists to all of your monthly archives. For example, www.yoursite.com/archives/.


Step 1 Copy Your Page.php Template

The best way to create any new WordPress page template is by copying the code from the themes page.php template and removing the code within the main content area. The Twenty Eleven page.php template looks like this:


Step 2 Create An Empty Archives.php Template

Copy your page.php template code to a new template file called archives.php. We will now remove the code that we do not need for our archives template.

The main content area of the Twenty Eleven page.php template simply displays the loop. All you need to do is remove those 4 lines of code. (i.e. remove the while statement).

Additionally, you should add the following:

  • Template Name: Archives Template - Add this to the top of your template to make it selectable as a page template in your page editor.
  • <?php the_title(); ?> - This will display our archives page title.
  • <?php the_post(); the_content();  ?> - This will ensure any content that is added to your Archives Page via the WordPress page area will be displayed at the top of the page (You do not need to add this line if you do not plan on writing an introduction before your archives).

This gives us the following:

You now have an empty archives template. It's time to populate it with some content :)


Step 3 Call Some Basic Functions In Your Archives.php Template

Most archives pages display a search form, monthly archives and category archives. These can be added to your archives.php template using the following 3 functions:

  • get_search_form();
  • wp_get_archives();
  • wp_list_categories();

Your archives.php template should now look something like this:


Step 4 Create An Archives Page

Your theme now has a basic archives.php template. The next thing you need to do is create a new page on your website (i.e. at http://www.yoursite.com/wp-admin/post-new.php?post_type=page). You can call the page anything you want e.g. Archives, Site Archives, Sitemap etc.

Make sure you change the template for the page from the default template to your newly created archives template.

If you followed this tutorial using the Twenty Eleven theme, your archives page should look something like this:


Bonus Step Customising Your Archives Page

In step 3 we added a search form, monthly archives and category archives to our archives page. The wp_get_archives and wp_list_categories functions that we used are very versatile and can be used to display a wide range of different archive lists.

Two other functions that you may find useful are wp_list_pages and wp_tag_cloud.

Below you will find some examples of the parameters that can be passed to all of these functions. You should find them useful in customising your archives.php template further.

wp_get_archives Examples

Displays the last 30 days of posts.

Displays the last 20 posts.

Displays weekly archives.

Displays 12 weekly archives.

Note, this doesn't necessarily list the last 12 weeks of posts. If will list the last 12 weeks that there were posts published i.e. the list could go back years if your blog hasn't been updated much.

Displays monthly archives with post counts.

Displays yearly archives.

Displays all posts alphabetically.

Displays all posts by date. Newest posts are listed first.

wp_list_categories Examples

Show post count of categories.

Change the name of the list title from 'Categories' to 'Info'.

Display empty categories in the category list.

Only list categories 1 and 5.

Do not list category 2.

Show only top level categories.

Show three level of categories (i.e. top category, child category and child of child category).

Only show the children categories of category 3

Only show category 4, show post counts and change the list title from 'Categories' to 'News'.

wp_get_archives Examples

List all pages and sub pages.

Display all pages and sub pages but no list title.

Sort pages by the date they were created.

Sort pages by the date they were last modified.

Only list the pages that you specify.

Exclude certain pages from the list.

Only display the top level of pages.

Display pages and the first level of sub pages.

wp_tag_cloud Examples

Display your tag cloud in full.

Order your tag cloud by count (least number of topics to most).

Set the text sizes for smallest and larger count values (8 is the default size for the smallest counts and 22 for the largest).

Display a tag cloud of your categories and your tags.


Conclusion

By adding an archives page to your website you are giving your readers and search engines more ways of finding your best content. It's such an easy page to set up that it's surprising that more themes don't come with an archives template included.

Remember, even though we have used the default Twenty Eleven as an example throughout this tutorial, the steps that we have taken can be applied to any modern WordPress theme.

Good luck - Kevin

Tags:

Comments

Related Articles