Add Category Title to Single Post

Supplemental 65 views
+1
0 Flares 0 Flares ×

When trying to show only the first categories name use:

<?php
$category = get_the_category($post->ID);
echo $category[0]->cat_name;
?>

If you want to make it a link use:

<?php
$category = get_the_category($post->ID);
if($category[0]){
echo '<a href="'.get_category_link($category[0]->term_id ).'">'.$category[0]->cat_name.'</a>';
}
?>

Note that this code will only work within the loop.

For Reference:

Add Category Title to Single Post

Provided by: Tiaan Swart
Posted in Wordpress

Discuss:

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

No Trackbacks.

Login

Register | Lost your password?
0 Flares Twitter 0 Facebook 0 Google+ 0 0 Flares ×