Reputation: 1251
How can i show the term name thats linked to the post of a post object (ACF)?
With this code i can see the title of that post:
get_the_title( get_field('which_game')->ID );
Upvotes: 0
Views: 1113
Reputation: 14312
To get the term names for a post, you can use either of these functions:
They work the same way but have slightly different argument lists, and more crucially get_the_terms
works on cached data (making it quicker) whereas wp_get_post_terms
doesn't.
$postObj = get_field('which_game');
// DEPENDING ON WHICH FUNCTION YOU WANT TO USE:
$terms = get_the_terms( $postObj->ID, 'provider');
// OR
$terms = wp_get_post_terms( $postObj->ID, 'provider' array( 'fields' => 'name') );
// Both functions return arrays, even if there is just 1 term
// so loop through the terms returned to add the names to an array
foreach ($terms as $term)
$term_names[] = $term->name;
// turn the array into a comma-separated string (of just the name on its own if there is just 1)
$term_name_str = implode("','",$term_names);
If you are sure you will only have 1 term you could just check that there are any results returned and then get the first one:
if (count($terms) > 1)
$term_name_str = $terms[0]->name;
However, the first example works well for just 1 term and is more flexible.
Note - this code is untested so it might have a few syntax errors, but the basic concept is correct.
Upvotes: 2