This website uses cookies to ensure you get the best possible experience. See our Cookies Policy.
Three Ways to Make Your WordPress Category Pages Better
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.
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.
Call this into your category.php file with category_description. I like to stick this in right below the first heading tag.
<?php echo category_description(); ?>
<?phpif(have_posts()):?>
<div class=“post clear”>
<h1><?phpsingle_cat_title();?></h1>
<?phpechocategory_description();?>
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>Subcategoriesof<?phpsingle_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 endif; ?>
<?php
$cats=get_categories(array(‘child_of’=>get_queried_object_id()));
if(!empty($cats)):
?>
<h2>Subcategoriesof<?phpsingle_cat_title();?></h2>
<ul>
<?php
foreach($catsas$cat)
{
echo‘<li><a href=”‘.get_category_link($cat->term_id).‘”>’.esc_attr($cat->name).‘</a></li>’;
}
?>
</ul>
<?phpendif;?>
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’);
functionpmg_add_thumbs()
{
add_theme_support(‘post-thumbnails’);
}
Stay in touch
Subscribe to our newsletter
Then, somewhere in the loop, you can call the_post_thumbnail to display the image.
Posted by: Christopher Davis