In your WordPress blog do you have a sortable clean and easy way for visitors to access your previous posts? Other than the month based archive link in your sidebar, you may not have an archive index at all! It’s time you hacked your own Master Archive Index Page!

This is WordPress Hack #2 in our 30 “Hack-a-Thon”! That’s right, I’m going to give you one WordPress hack per day for 30 days, and at the end of that time you will have pimped out your blog like you never would have believed possible! Be sure to signup for the RSS feed or subscribe by email at the top of the page.

Out of the box the default Archives for WordPress aren’t very sexy. In fact, as I’ve mentioned in previous posts the word thing about WordPress (to me) is the fact that the content seems hard to navigate, hard to get to, and downright self-defeating at times. Let’s take this blog as it stands right now as an example. The homepage has one full post and 9 excerpted posts. The sidebar contains links to the last 10 posts, and most viewed posts, in addition to a link to categories and monthly archives. I can also search, and those are my basic options for finding content. The custom pages I’ve setup also lead to some article series and categorized content. I currently don’t have an HTML web page site map (just an XML sitemap for search crawlers), so there really is no way to get a birds eye view of all my content. I’m rapidly approaching 100 posts, but at any give time you’re lucky to have more than 10-15 options for content as you surf around my blog.

Your theme may or may not currently display any more than month based archive pages, which will just be links in your sidebar to the months of the year you had posts. Some themes also have a calendar. All these normally do is use your “archive.php” in your theme directory as a template to bring up a list of posts the same way you index, search, category, or tag pages do – a list of up 10 to posts at a time with ‘previous’ and ‘next’ links at the bottom of the page. In my experience, most themes don’t have a master archive page (or what the WordPress Codex calls the “Archive Index”, although some have a “archives.php” file to create one from. Notice that this template file has an “s” on archives.php. Look in your theme directory, and you should have a file named “archives.php”. If you do add the first 5 lines below at the very top of that file, and the rest of the code in “the loop” of that page (after the header and before the endwhile). If for some reason your theme doesn’t have an archives.php, just download a copy of your “page.php” file, rename and save it as “archives.php” and then add these code to it (then upload to your theme directory).


<?php
/*
Template Name: Master Archives Page
*/
?>

<h2>Archives by Month:</h2>
  <ul>
    <?php wp_get_archives('type=monthly'); ?>
  </ul>
	<br />
<h2>Archives by Subject:</h2>
  <ul>
     <?php wp_list_cats(); ?>
  </ul>

page template example Make sure you have the “Template Name” in the top. Now upload this file to your theme directory, and in your WordPress dashboard go to “Write -> Page”. On the right hand side you’ll see “page template” as in the image example here. Just select “Master Archives Page”. I’m going to call mine “Archived Posts Sitemap”. I like that title because it says it’s an archive page with a sitemap. To me a sitemap is a great big categorized page of titles and links where you can get a the big picture of a site pretty quickly. That’s the impression I want to give people, unlike the monthly archive pages that just list posts and excerpts. The first time creating this page I’m not going to give it much content – just some introductory content. Save this page and then reload your site in another tab or window. You should see the name of this new page in your “Pages” in your sidebar. Click on it to bring it up and you should have something like this:

archive index example

You see in that example what you get is a listing of archives by month, and also by subject (category). This really sucks doesn’t it!? I mean come on, everyone already has both of these already in their sidebar – I do! There are basically two different ways to solve this problem, and actually these solutions can be combined. The first thing we can do is get a WordPress plugin for archives to get more options on this page. The second is to do a little hackng and add some code to the page to get the information out of the database we need to make sure visitor’s get all the options they need.

After reviewing my options – these are my thoughts…

I “could” use yesterday’s query_posts hack to list every post I have on the page – but that wouldn’t be very nice visually at all. I think it would be great to give visitor’s most “most viewed” or “most popular” posts on this page. I already have the most viewed top 10 in my sidebar. I use the Lester Chan Plugin WP-PostViews to get this. It will display the most viewed posts, or most viewed posts for a category. I’m going to use it to display the top 25 most viewed posts. That’s more than I have in my sidebar and should be very useful to visitor’s. According to the WP_PostViews documentation all I need to do is add this code “outside the loop” of my archives.php to add this to my Master Archives Index Page (since I already ahve WP_PostViews installed as a plugin in my WordPress installation):

<h2>Top 25 Most Viewed Posts</h2>
<?php if (function_exists('get_most_viewed')): ?>
   <?php get_most_viewed('both', 25) ?>
<?php endif; ?>
<br /><br />

Since you have to use that code outside the loop, and I have a page heading just inside the loop on my archives.php, I decided to place this after the loop in my Master Archives Index page. Another way to do this you might care for is by using the using the Top Post plugin by JohnTP to display the top post by category like he does on his top posts page. I decided to stick with what I have so far.

Doing a little more research I found two very similar WordPress Archiving Plugins, SRG Clean Archives, and Smart Archives. They basically do the same thing, just formatted a little differently.

Here’s a clean archives example where you’ll see that you get monthly archives with linked titles and post counts. It also shows the day posted before and comment count after each linked title.

clean archives example

In the Smart Archives plugin example you’ll see an addition of a linked calendar above the posts by year and month. The posts are also listed by month. With this plugin you don’t get date posted or comment count.

smart archives example

In the end I wanted to go with something I thought was not only a bit more cool, but much more user-friedly. I really wanted to use Exended Live Archive because of the interface, but it’s not compatible with WordPress 2.3+ at all.

In the end I went with KG Archives for my Master Archives Index page because it has a handy drop down at the top where visitor’s can quickly choose to see one month or all months quickly of archived posts. In addition this post lists the date posted and comment count for each as well.

KG Archives example

To place the KG Archives content on your Master Archive Index Page you just have to copy and paste the following code – but this time go back and “edit” your actual “page” in “Manage->Pages” where you wrote the intro content, and paste this beneath (in code view):


<kgarchives initialmonth="current" />

I removed the original code I added at the beginning of this hack tutorial to get archives by month or subject in favor of the KG Archives sortable archive code. Based on all the options I presented to you in this post – you must decide which of all the layouts and plugins you like best for your archives index page. View my new Archived Posts Sitemap page here!

There are many other options in the readme file if you download this plugin, I chose to only display the current month at first. Now I have a Master Archive Index Page where users can navigate all my past posts quickly and easily. They can even see all posts on one page if they choose. I’ve also listed my most popular posts. If I keep adding pages, I may also expand my Archive Index to include a listing of all my parent and child pages at the very bottom. Create your own archive index now! I gave you plenty of options, and as always if you have something to add or a question – please comment now!