Blah, blah, blah.

What's all the hub bub, bub?

What Do We Have To Say?

Apr
29
2009

WordPress How-To: Display Category Name Based on Slug

I’ve been developing sites on WordPress for awhile now, and I love it for a number of reasons. It’s fully customizable, has an incredibly intuitive and easy-to-use interface, and the thousands of plugins available pretty much cover all the bases for any functionality you could possibly need.

I’ve also been learning lots of nice little tricks for customizing themes. I plan to occasionally post little tutorials describing some of these tricks. Hopefully, someone will find some use for them, and save themselves a little time!

One trick that I just used and that took forever to find is the ability to display the current category name somewhere on the page. Specifically, I wanted to be able to output this in an <h1> tag on my category.php page. I tried a plugin that was supposed to provide additional template tags that aren’t included in a native WordPress installation. That didn’t work; it kept outputting the default category. I tried lots of thing, and finally found the solution I was after.

<?php single_cat_title(''); ?>

Pretty simple, right? This will simply display the category name on the page based on the category slug on the current page. If you want to add some text to display before the category name, you would use:

<?php single_cat_title('You are reading about '); ?>

Which, if the current category was WordPress, would look like this:

You are reading about WordPress

Alternately, you can assign the category name to a variable for use in your code. That would look something like this:

$current_category = single_cat_title("", false);

Pretty cool, huh?