Damian Caynes
Damian Caynes

Reputation: 1

Displaying the first category from a custom taxonomy with get_the_terms

I have setup a custom post type with a custom taxonomy as categories.

I need to use get_the_terms or wp_get_post_terms to return the first category of a post in the custom post type.

But I can't quite figure out how to use it in comparison to get_the_category.

Upvotes: 0

Views: 396

Answers (1)

samisonline
samisonline

Reputation: 188

https://codex.wordpress.org/Function_Reference/wp_get_post_terms

This is a very useful resource, if you are within the loop already it will return an array of categories. To access the first one you could assign the array to a variable and call the first one.

$arr = wp_get_post_terms();
$first = $arr[0];

Or the last

$last = array_pop((array_slice($arr, -1)));

Upvotes: 1

Related Questions