Top

How to Create an Archives Page For Your WordPress Blog

January 24, 2007 by  

You may have noticed that my sidebar is now shorter and less cluttered then it was previously. I’ve removed the Categories and Monthly Archives sections and moved them into a new page which I called Archives.

There are several ways to do this and once of which is explained at WordPress.org.

Their suggestion involves creating a new template page called archives.php, customizing the archive template and setting up redirects with your .htaccess file.

Perhaps it’s a more complete way of doing this but some people especially newbies may find it a little complicated or it involved too much work.

So I came up with a more straight forward solution. I must warn you though that I am not a PHP programmer, my solution is not perfect but at least it gets the job done.

To achieve my goal, I needed to run some PHP code on the WordPress page and a plugin called PHPExec helped me to do this. Note that if you are using WordPress 2.0.x you’ll need to use the patched version to make this works.

Also I haven’t tested this with WordPress 2.1 so do it at your own risk if you decide to install it on that version.

Here’s what you have to do.

 

1. Install PHPExec and activate the plugin.

2. Create a page (not a post) as you normally would from you WordPress control panel.

3. Cut your Categories code section from your sidebar and paste it onto the new page. My code originally looked something like this.

 

<h2><?php _e('Categories'); ?></h2>
<ul>
<?php wp_list_cats('sort_column=name&optioncount=1&hierarchical=1&children=1'); ?>
</ul><?php $link_cats = $wpdb->get_results("SELECT cat_id, cat_name FROM $wpdb->linkcategories");
foreach ($link_cats as $link_cat)
?>

 

4. Then edit the code, enclose it with the <phpcode></phpcode> tags so that the PHP code can run on the page. The resulting code looks like the followings:

 

<phpcode><?php
global $wpdb;
echo "<h2>";
_e('Browse By Categories');
echo "</h2><ul>";
wp_list_cats('sort_column=name&optioncount=1&hierarchical=1&children=1');
echo "</ul>";
$link_cats = $wpdb->get_results("SELECT cat_id, cat_name FROM $wpdb->linkcategories");
foreach ($link_cats as $link_cat);
?></phpcode>

 

Note that you need to declare global $wpdb; at the begginning of your code otherwise you’ll get an error.

5. Click Save and you are done!

With this technique, you can easily add other archive formats such as archive by month or your top 15 post etc.

Anyway, if you have suggestions on how I can improve this, please leave your comment.

update: I’ve upgraded to WordPress 2.1 and my Archives page works fine, so PHPExec should be compatible with WordPress 2.1

Did you enjoy this post? Please subscribe via RSS or email.

Related posts

Comments

RSS feed | Trackback URI

2 Comments »

Comment by ShaolinTiger
2007-01-24 12:39:53

Quite cool, still prefer my separate archive and popular pages tho :)

 
Comment by Lorelle
2007-01-26 03:52:44

You might want to offer examples for WordPress 2.1 users using the new wp_list_categories() template tag. wp_list_cats() has been deprecated.

Good example of how to customize your archives page, as well as a good example of a site map. Thanks.

 
Name (required)
E-mail (required - never shown publicly)
URI
Subscribe to comments via email
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> in your comment.

Trackback responses to this post

Bottom