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

[ad_1]

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

<?php 
$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.

[ad_2]

Leave a Reply

Random Post Selection ::

Recent Posts

Unlock Unlimited Downloads