Home › Forums › Front-end Issues › Implode Post Object with "," and add "and" at last word › Reply To: Implode Post Object with "," and add "and" at last word
I don’t see why it wouldn’t work in principle as long as you have at least 2 items in the array. But it will fall apart when there are none or 1. Although I do not understand your purpose for using array_filter()
and filtering all the values through strlen()
as this would return an array of the string lengths of the elements.
$development = the_sub_field('development', $post_id);
$output = false;
if (count($development) == 1) {
$output = $development[0];
} elseif (count($development) > 1) {
$last = array_slice($development, -1);
$first = join(', ', array_slice($development, 0, -1));
$both = array_merge(array($first), $last);
$output = join(' and ', $both);
}
if ($output) {
echo $output;
}
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.