Home › Forums › Front-end Issues › Custom taxonomy slug not ID (again, i know) › Reply To: Custom taxonomy slug not ID (again, i know)
A taxonomy field by default allows multiple terms to be selected, which means that it will return either an array of IDs or an array of terms depending on what you have it set for. Unless you change it to a field type that only allows a single value.
It is best to never assume what is being returned. It could be an array or a single value. The reason for this is that let’s say that you create a taxonomy field that allows multiple selections and you edit a few posts with this setup. If you then go in an change it to only allowing one selection the values in the posts you already edited will still contain arrays unless you go back and edit every post. Meanwhile any new posts you add will have just a single value.
// if you are expecting a single value
$term = get_field('your_taxonomy_field');
if (is_array($term)) {
$term = $terms[0];
}
echo $term->name;
// if you are expecting an array
$terms = get_field('your_taxonomy_field');
if (!is_array($terms)) {
$terms = array($terms);
}
foreach ($terms as $term) {
echo $term->name;
}
If that doesn’t completely answer your question post the code you’re using.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.