How to get the category title in a post in Wordpress?

Keith Donegan picture Keith Donegan · Jun 3, 2009 · Viewed 105.1k times · Source

Say I have a post called Hello World in Wordpress and I'm directly viewing this page, how would I go about finding the category of "Hello World" and displaying it?

Answer

RichieHindle picture RichieHindle · Jun 3, 2009

Use get_the_category() like this:

<?php
foreach((get_the_category()) as $category) { 
    echo $category->cat_name . ' '; 
} 
?>

It returns a list because a post can have more than one category.

The documentation also explains how to do this from outside the loop.