How To *Easily* Make WordPress Archives That Rock - Smashing WordPress Tutorials #1
Yo there! If you are new here, you may want to subscribe to my RSS feed, or through email, for more tips on better and effective blogging!
Today’s Sunday, and here’s a nifty little tutorial for you to enjoy, while you relax out. So, enjoy! And oh, this post is Part 1 of Smashing WordPress Tutorials. The next tutorial will be on automatically updating pages. Subscribe to the RSS feed to be the first to know of new content!
The archives of your blog are like a museum, which store all your posts, for anyone to view. An archives page is an essential part of a blog, so if you don’t have one, you should consider creating one. If you don’t know how to, you’ve come to the right place!
Define Your Aims First
The first thing to do is to plan out your archives. What are you going to show on the archives page, and how? Here’s a possible combination that is simple yet effective, which I also use on PuttingBlogsFirst
- A monthly(or yearly) list. Visit the PBF archives for example.
- ALL the posts ever on your blog.
- Optional: A list of all the categories. I don’t use this, because the list of categories is already present in the sidebar. *shrug*
Now that we’ve got our goals defined, let’s start building!
The Archives Page
Before we actually start building our archives, we want an archives.php file.
We’ll also have to create a Page in WordPress for this. More on this later.
Now, I’m assuming you already have a barebones master template like similar to this(save it as archives.php):
<div id=”container”>
<?php if (have_posts()) : ?>
<div id=”content”>
</div>
<?php endif; ?>
</div>
<?php get_sidebar();?>
<?php get_footer();?>
Now, in this case, all my code would go between <div id="content"> and
</div>. Find the place in your theme.
The WordPress Page
None of this will work unless you create a WordPress Page for your archives. So, first, add these lines to the very top of your archives.php page:
/*
Template Name: Archives
*/
?>
Now, go to the Write Page option in your WordPress admin, and name the page something descriptive, such as ‘Archives’. Do not write anything in the content area.
Next, scroll down to the Page Templates option, and select the ‘Archives’ one. And then save the page, and enjoy this tutorial!
The Code Itself
First, we have to list the archives ordered by month. For that, we use the wp_get_archives() template tag. Here is the complete code(with some organization):
<ul class=”archivelist”>
<?php wp_get_archives(’type=monthly&show_post_count=1′) ?>
</ul>
First, I put in a level 3 heading, telling people that these are the monthly archives. The type is set to monthly and it shows the archives in monthly format. The show_post_count parameter is set to 1(ON), which means that the number of posts in that particular month are shown in brackets beside the month name.
I’ve also enclosed this tag in an unordered list (<ul>), because this tag generates list items (<li>) without being enclosed in a(at least when used like this). You can improve the look of these archives even further using CSS and divs, but that’s another story
TIP 1: To display the archives ordered by year, instead of months, change the type parameter to yearly. For a daily listing, change it to daily. For a weekly listing, change it to weekly
Showing ALL Posts
Now, we show all the posts ever, on your blog. There are several template tags here to do the job, but I’m going to be using the same one as the above, wp_get_archives(), since it’s very easy to use. So, here’s the code to use:
<ul class=”archivelist”>
<?php wp_get_archives(’type=postbypost’);?>
</ul>
Again, I use a level 3 heading to tell the users what this big list is all about. Again I enclose the tag in an unordered list. The type parameter is set to postbypost, so all posts are displayed by post title, with the most recent one first.
TIP: If you wanted to limit the listing to, say, only the most 20 recent posts, add limit=20 to the paramaters, like so:
And you’re done!
Archives Ordered By Category
Another way to show your archives is to show it by category(like in the sidebar of this blog). The template tag for this job is wp_list_categories(). Here you go:
This will output all the categories there are on your blog. Also, the show_count parameter is set to 1(ON), so this also displays the number posts in ever category next to the category name.
And you’re done!
What do You Think?
Try out this and make your archives ROCK!
If you run into any problems, do let me know!
The next tutorial will be on automatically updating pages. Subscribe to the RSS feed to be the first to know of new content!
If you liked this post, consider subscribing to the RSS feed (what's RSS?) to get updates on new blog posts.










This is great! Is it possible to list post-by-post for each month? For example:
August 2008
- Post 3
- Post 2
- Post 1
July 2008
- Post 3
- Post 2
- Post 1