How to get category of current post

June 21, 2018

Following code can be used to get the category of the current post which you are dealing with

You can use the get_the_category( $post->ID ) to get the category. What you have pass here is the ID of the post. You can get the ID of the post using the $post global variable

global $post;
$postcat = get_the_category( $post->ID );

if ( ! empty( $postcat ) ) {
    echo esc_html( $postcat[0]->name );   

Use print_r($postcat) to see what is coming with category object