Web Development Blog

by Ei Sabai Nyo

14 May, 2007

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: , , ,

Love what you've just read? Subscribe to our newsletter to receive tips, resources and special offers related to web development & design.
Your name:   Your email:  

5 Responses to “Customising category.php in WordPress”

  1. CraZy675 says:

    Holy ads, I put my adblocker to use today.

    you should also put your code into tags and format it.

  2. Tahsin says:

    I also use wordpress, but i didn’t face any problem.

  3. Hey, thanks for this post! I didn’t know to use “single_cat_title” but your post helped.

    Much appreciated!

  4. Ivan says:

    Very nice tutorial, thanks.

  5. Thanks for this information on Customising category.php in WordPress.

Profile PicHello! 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 about me or have a look at some of the tips & resources I've written.
Subscribe to our newsletter to receive tips, resources and special offers related to web development & design.
We do NOT spam.
Your name:  
Your email:  

Tips & Resources

Tips & Resources
WordPress Web Hosting
Recommended web hosting providers for WordPress 3.0
iPhone Native App Development
Important steps into iPhone app development for beginners
iPhone Web App Development
Tips for iPhone web app development
Coupons for Web Developers
Get discounts on web hosting, domain names, templates, etc
10 Useful jQuery Snippets
Easy-to-use jQuery snippets for any website
HTML Email Newsletter
Step-by-step tutorial on how to code an HTML email newsletter
  • bluehost Hosting $6.95/month
  • Joomla Templates

Recommended Book

Categories