WordPress – Get Category Name of Current Post

This post shows you how to get category name of the current post in WordPress.

To do this, we can use get_the_category() function as shown right below.

<?php

global $post;
$current_post_id = $post->ID;
$category = get_the_category( $current_post_id );

if ( ! empty( $category ) ) {
    $category_name = $category[0]->name;
}

In the above code, we first get the current post id of the post using $post->ID, then pass it to the get_the_category() function to get the category data, and finally get the category name via name property.

0 0 vote
Article Rating
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x