How can I get the category-name or slug out of a custom taxonomy field?
When I use the following code, I get the cat ID. But if i switch in the backend the return value from “term ID” to “term object”, my template crashes.
Can anybody help? I’m not that fit in php an I already read the taxonomy page in the documentation.
`<?php the_field(‘field_name’, $term); ?>’
If you’re returning an term object then you need to use get_field()
and use it rather than the_field()
which echos the returned field.
<?php
$term = get_field('taxonomy_field');
echo $term->name;
?>
Thanks John.
Sorry, I don’t get it. I replaced ‘taxonomy_field’ with my custom field name ‘instrument’. But that’s not correct, right?
Attached is a screenshot of this custom field.
You are returning the term ID, it’s not easy to get the term name from the term ID without a bit of work. If you want to display the term name you should set the field to return the term Object.
Hooray! So it was the combination of setting it to “term object” and your snippet above. (… and I had a little crumb of crab in one line.)
You saved my day. Thank you very much.