Hi, I am using a Multiple select field to see what classes to apply to an image.
Anyways, in the back end, say I have these items selected:
- classroom
- Performance
- Culture
The Output I get is
Classroom, Performance, Culture
how do I remove the commas from the output?
You’re using the_field()
for a field type that stores an array, which is automatically imploding the array with ', '
as a separator. You need to use get_feild()
and do the imploding yourself, as well as the logic involved in testing to see if anything is returned.
$classes = '';
$value = get_field('select_field_name);
if ($value) {
$classes = implode(' ', $value);
}
echo $classes;
Thank you that did exactly what I wanted. I didn’t realize get_field was going to output something different.
To be honest, I didn’t know the the_field() and get_field() worked slightly differently until I saw your question. But then I thought about it because I know that multi select fields store arrays and since the_field() outputs the values of a field I decided to look at the function to see how it treats arrays. So I got to learn something. 🙂
Hello!
I have some problem.
Where can I find it the_field()
? I can’t understand how remove commas 🙁
Some fields store arrays, fields like checkboxes, multi select and others. When you use the_field() for fields that store arrays ACF converts the array to a string and puts a comma between each element. For these types of fields, it you want to format the values in some other way then you need to use get_field() and then loop through the array yourself. These concepts are all covered in the documentation for each type of field that needs special consideration, like checkboxes https://www.advancedcustomfields.com/resources/checkbox/