jmccole83
5/23/2018 - 3:10 PM

WordPress | Get first category name, slug and ID

Use on of the below examples to get the first category in an array, echo the correct variable for the desired content.

// in the loop
$category = get_the_category();
$currentcat = $category[0]->cat_ID;
$currentcatname = $category[0]->cat_name;
$currentcatslug = get_category_link( $currentcat );
// outside the loop
global $post;
$categories = get_the_category($post->ID);
$currentcat = $category[0]->cat_ID;
$currentcatname = $category[0]->cat_name;
$currentcatslug = get_category_link( $currentcat );