Home › Forums › General Issues › checkbox field output
I am working on a repeater and one of my sub fields is a checkbox
<?php the_sub_field(‘series’); ?>
how can i get it to show the label, not the value?
and how to i get it to not make them comma separated? i would like them to have another character as the separator.
1) ACF Pro has a setting for checkbox field to return the value, label or both.
2) Instead of using the_field()
$values = get_field('your_field');
echo implode('; ', $values);
// or even something like
echo '<span>',implode('</span><span>', $values),'</span>';
Sorry for the late response. i got
Warning: implode(): Invalid arguments passed
I checked, and the field is returning an array
I figured it out!
<?php $series = get_sub_field(‘series’); ?>
<?php foreach ($series as $serie): ?><?php echo $serie[‘value’]; ?><?php endforeach; ?>
output label like this
<?php foreach ($series as $serie): ?><?php echo $serie[‘label’]; ?><?php endforeach; ?>
$values = get_field('your_field');
echo implode('; ', $values);
returns Warning: implode(): Invalid arguments passed
@braddalton what is the return value setting for the field? Does the field have a value? The code I provided was just a simple example and assumes that you are returning just values or just labels and not both values and labels and does not include any error checking.
Thanks for the quick reply John
return value is value
The checkbox has multiple choices as follows
Natural
Denatured
Bacterial over expression
Insect cell over expression
Embryonic kidney extract
Other
However, no default values have been set.
I also used the example from your documentation under the Display Value sub heading and it returns another error
the_field('colors');
Notice: Array to string conversion
I assume this can be fixed by specifying both a value and label like this:
red : Red
However that will output the label for each when whats needed is comma delimited values only.
The code should work with the return value set as “value”
// with checking
if (get_field('colors')) {
echo implode('; ', get_field('colors'));
}
The topic ‘checkbox field output’ is closed to new replies.
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.