How to Create an Archives Page For Your WordPress Blog
January 24, 2007 by Gaman
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>
<?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:
_e('Browse By Categories');
$link_cats = $wpdb->get_results("SELECT cat_id, cat_name FROM $wpdb->linkcategories");
foreach ($link_cats as $link_cat);
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