Template to Show Archive by Category

On developing a theme for wordpress, one must reach the situation where there’s no build in wordpress template tags to do specific task. They have to decide if they have to search for available plugin, creating a new plugin or the easiest way, creating a template for use with page. Templates are the files which control how your WordPress site will be displayed. Through its powerful Theme system, WordPress allows you to define as few or as many Templates as you like all under one Theme. In my case, I just need a template to show a category archive with links to posts under each category. Simple, huh?

Creating the Template File

First of all, I need to create a .php file for the template which I will use to display the archive. On the top of the blank file, I type this code so it will get recognized by the wordpress system as a template file.

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

Read : http://codex.wordpress.org/Stepping_Into_Templates

Getting the Categories and How to Display it

I need to get all the categories which have posts in my archive. Since it’s easier to use get_categories() tag, I have decided to use one. So I added this script inside my template file which will display a list of categories which link to each category and show total post for each category.

$categories = get_categories();
foreach( $categories as $cat ) :
	// Get the category ID
	$cat_ID = get_cat_ID( $cat->cat_name );

	// Get the category link
	print '<h3>';
	print '<a href="' . get_category_link( $cat_ID ) . '">';
	print $cat->cat_name;
	print '</a>';
	print '(' . $cat->category_count . ')';
	print '</h3>' . "\n";

	// Get the category description
	print '<p>' . $cat->category_description . '</p>' . "\n";
endforeach;

Read : http://codex.wordpress.org/Function_Reference/get_categories

Getting Latest Post for Each Categories

I came up with the solution to use database class to retrieve the post data from the database. What about get_pages()? That’s just not works for this purpose. Here’s what I’ve wrote to retrieve such post data from the database:

	// Query the taxonomy
	$the_terms = $wpdb->get_results("
		SELECT wp_term_taxonomy.term_taxonomy_id as the_term_id
		FROM $wpdb->term_relationships
		JOIN $wpdb->term_taxonomy
		ON $wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_id
		WHERE $wpdb->term_taxonomy.term_id = $cat_ID
		AND $wpdb->term_taxonomy.taxonomy = 'category'
		GROUP BY $wpdb->term_relationships.term_taxonomy_id
	");

	// Get tge term ID
	$the_term_ID = $the_terms[0]->the_term_id;

	// Query the latest post for each categories limited to three result
	$the_posts = $wpdb->get_results("
		SELECT *
		FROM $wpdb->posts
		JOIN $wpdb->term_relationships
		ON $wpdb->posts.ID = $wpdb->term_relationships.object_id
		WHERE $wpdb->posts.post_type = 'post'
		AND $wpdb->posts.post_status = 'publish'
		AND $wpdb->term_relationships.term_taxonomy_id = $the_term_ID
		GROUP BY $wpdb->posts.ID
		ORDER BY $wpdb->posts.post_date DESC
		LIMIT $limit
	");

Now I need to display each post for current category:

	// Show the latest posts
	print '<ul>' . "\n";
	foreach ( $the_posts as $the_post ) {
		print '<li class="archive-post-title">';
        print mysql2date( get_option( 'date_format' ), $the_post->post_date );
        print ' : ';
        print '<a href="' . get_permalink( $the_post->ID ) . '">';
        print $the_post->post_title;
        print '</a>';
        print '</li>' . "\n";
	}
	print '</ul>' . "\n";

Read : http://codex.wordpress.org/Function_Reference/wpdb_Class

Using the template

I wrapped up those codes into single file named categories.php, added some styling, uploaded it into the theme directory, create a new ‘page’ named ‘Archive by Category’ and use this template on the ‘page’.

Download Archive by Category 0.1 [800 bytes]
See how it performs

Leave a Response