Get Category name from Post ID

user1937021 picture user1937021 · Jun 25, 2013 · Viewed 162.2k times · Source

Is it possible to get the category name of a category given the Post ID, the following code works to get the Category Id, but how can I get the name?

<?php $post_categories = wp_get_post_categories( 4 ); echo $post_categories[0]?>

Thank!

Answer

M Khalid Junaid picture M Khalid Junaid · Jun 25, 2013

here you go get_the_category( $post->ID ); will return the array of categories of that post you need to loop through the array

$category_detail=get_the_category('4');//$post->ID
foreach($category_detail as $cd){
echo $cd->cat_name;
}

get_the_category