Three Ways to Make Your WordPress Category Pages Suck Less

By

Let me be honest with you: your WordPress category pages suck. It’s okay, it’s not your fault. Most theme designers don’t realize the value of a good category pages. Here are three ways to make yours better.

1. Add Unique Content

WordPress comes with a feature few theme developers make use of: the taxonomy description. It’s a short bit of text that describes your taxonomy (category, tag or custom taxonomy), and you can see the input for this thing on the “edit category” or “edit tag” page.

taxonomy description input

Call this into your category.php file with category_description. I like to stick this in right below the first heading tag.

<?php if (have_posts()) : ?>
<div class="post clear">
	<h1><?php single_cat_title(); ?></h1>

	<?php echo category_description(); ?>

2. List Subcategories

If you run a larger site with many subcategories, you should provide a way for your visitors to find their way around. Listing subcategories is one way to that.

So right below your category desciption, add some more code. First, we need to get a list of categories that are children of the current category. For this, we’ll use wp_list_categories.

<h2>Subcategories of <?php single_cat_title(); ?></h2>
<?php
wp_list_categories(
	array(
		'child_of' => get_queried_object_id()
	)
);
?>

This would work, except “Subcategories of [Some Category]” will always be on every category page regardless of whether it had subcategories or not. Instead, let’s change our code a bit to use get_categories, which will return an array. Once we have that, we can check to make sure we actually have categories before printing anything to the page. Then we loop through each category, getting the link with get_category_link. Note that get_categories returns an array of objects, so we already have the category name ($cat->name) and ID ($cat->term_id) for each category.

<?php
$ cats = get_categories( array( 'child_of' => get_queried_object_id() ) );
if( !empty( $cats ) ):
?>
<h2>Subcategories of <?php single_cat_title(); ?></h2>
<ul>
	<?php
	foreach( $cats as $cat )
	{
		echo '<li><a href="' . get_category_link( $cat->term_id ) . '">' . esc_attr( $cat->name ) . '</a></li>';
	}
	?>
</ul>

<?php endif; ?>

3. Add Images

If you’re not using WordPress’ built in post thumbnails, you should be. From an SEO perspective, images add a valuable aspect to the page. So why not use them?

Enable post thumbnails in your theme by adding the following your your functions.php file

add_action( 'after_setup_theme', 'pmg_add_thumbs' );
function pmg_add_thumbs()
{
	add_theme_support( 'post-thumbnails' );
}

Then, somewhere in the loop, you can call the_post_thumbnail to display the image.

Posted on July 22, 2011 in WordPress.