The Article

Getting WordPress archives the way you want them

With this blog, I needed a little more functionality out of WordPress than just the standard install. One of those little things was more control over archives. As I was making the archives template page, I knew that wp_get_archives just wasn’t going to cut it for me in terms of display options. My plan was to choose one of the many plugins, but I realized that this could be a good excuse for me to have some fun and learn more about the CMS.

The archives page

I knew I wasn’t going to be able to use wp_get_archives on the template because I needed more data out of it. With a lot of help from the wordpress guy, I did something very similar but separated the year and the month. I also show an icon of the category it was posted in.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
post_date);
 
		// Get the month of the new post
		$new_month = mysql2date('M', $post->post_date);
 
		// If the year is new
		if ($year != $new_year) {
			$year = $new_year;
			$month = $new_month;
 
			if ($ul_open == 1) {
				// Close
<ul>
	<li>
<ul> tag
			}
			// Output $month and post data
			$ul_open = 1;
 
		// Else, keep outputting posts
		} else {
			// Output just the post data
			$ul_open = 1;
		}
	endforeach;
?&gt;</ul>
</li>
tag
			}
			// Output $year, $month, and post data
			$ul_open = 1;
 
		// If the month is new
		} elseif ($month != $new_month) {
			$month = $new_month;
 
			if ($ul_open == 1) {
				// Close</ul>

What I am doing here is fairly simple. First I am using get_posts to get my dataset of blog posts. I then set variables for the year and month of the current post. I check against those variables to see if the year or month has changed and output accordingly. The variable $ul_open is used to check if the list is open when outputting a new year or month.

This code can be used for other things as well because it is simply a loop though all the posts. You could sort them by anything: category, tags, custom fields, etc. You can be creative with your archives page, or any other page for that matter.

But Jake…why?

I am not a professional PHP developer, so this is the way it worked for me. If there is a better or faster way, by all means leave a comment and let me know. I am always looking to learn a thing or two. Also, feel free to use this code for use in your own theme if you wish.

There are 0 comments on “Getting WordPress archives the way you want themLeave one yourself

Leave a comment

My name is , my email address is ,

and my website address is .

My response:

(Some HTML is allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped=""> )