Customising category.php in WordPress
I am using WordPress as a CMS for one of my websites. For that website, instead of having a default category page, I wanted to have a customised category page which lists titles of all posts under that category. And I also wanted to display a category image if it exists in /wp-content/uploads/images folder. And because there was no plugin which satisfies my requirements, I created a custom category.php file in my theme folder. Here’s a snippet of my customised category.php.
<?php if (have_posts()) : ?>
<h2><?php single_cat_title(); ?></h2>
<?php
$current_category = single_cat_title("", false);
$image = '/wp-content/uploads/images/' . strtolower(str_replace(' ', '-', $current_category)) . '.jpg';
if (file_exists(ABSPATH . $image)) {
echo '<img src="' . get_bloginfo('url') . $image . '" alt="' . $current_category . '" />';
}
?>
<ol>
<?php while (have_posts()) : the_post(); ?>
<li>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
<?php the_title(); ?></a></li>
<?php endwhile; ?>
</ol>
<?php endif; ?>
What does the above code do?
1. Print out the title of current category
2. Assign the title of the category to a variable called current_category
3. Specify a relative file name for category specific image where image name is the title of the category in lower case with spaces being replaced by hyphens (-)
4. Check whether the specified image above exists on server
5. If it does, print out the image
6. In the post loop, print out titles of all posts under the current category in an ordered list with a link to the individual post page
Technorati Tags: wordpress, plugin, category.php, custom template
Other similar posts that you might be interested in:
- Display 5 latest posts in each category in WordPress
- 5 steps to creating a custom Archive page in WordPress
- Display 10 recent post titles on homepage
- How to customise WordPress Admin Login page
- How to add a breadcrumb to your blog and have it appear on Google’s Search Result Snippet
- A list of all posts in an alphabetical order
- How to display a different header image for different pages in WordPress
Hello! Welcome to Web development blog! My name is Ei Sabai and on this blog, I write about web development, mobile app development, latest web technologies and the likes. Read more 


Holy ads, I put my adblocker to use today.
you should also put your code into
tags and format it.I also use wordpress, but i didn’t face any problem.
Hey, thanks for this post! I didn’t know to use “single_cat_title” but your post helped.
Much appreciated!
Very nice tutorial, thanks.
Thanks for this information on Customising category.php in WordPress.