Get First Category By Post ID In WordPress (Outside The Loop)


Use this code snippet to display the first category of a WordPress post.

$smello = (array) get_the_category($post_ID])[0];
echo $smello['cat_name']; 

where $post_ID is the ID of the post whose category is required.

Change [0] to [1] to get the second category. Not all posts will have a second category, so you will have to check if its empty or not before using it.


Leave a Reply

Random Post Selection ::

Recent Posts

Unlock Unlimited Downloads