Home › Forums › Add-ons › Repeater Field › List all repeater subfield values for a post (preferably comma separated)
This seems like such an easy thing to accomplish, so I’m sure I’m just missing it somehow.
What I’m looking to do is list all repeater field (line_chart_all_months) subfield values (line_chart_month) as a comma separated array (each value in single quotes).
For example, each subfield is a text input with a value such as Aug-22 (a total of 12, ending in Jul-23). The desired return needs to look like this:
‘Aug-22′,’Sep-22′,’Oct-22′,’Nov-22′,’Dec-22′,’Jan-23′,’Feb-23′,’Mar-23′,’Apr-23′,’May-23′,’Jun-23′,’Jul-23’
How is this accomplished?
I’ve tried:
<?php if(have_rows('line_chart_all_months') ): while(have_rows('line_chart_all_months') ) : the_row(); the_sub_field('line_chart_month');
endwhile; else : endif; ?>
But it returns this:
Aug-22Sep-22Oct-22Nov-22Dec-22Jan-23Feb-23Mar-23Apr-23May-23Jun-23Jul-23
How do I get those separated with commas and single quotes for each value like above?
Thanks kindly in advance.
<?php
$dates = array();
if (have_rows('line_chart_all_months')) {
while (have_rows('line_chart_all_months')) {
the_row();
$dates[] = get_sub_field('line_chart_month');
}
}
echo implode(', ', $dates);
?>
Thanks very much for the help John!
This almost gets me there, however, I’m missing the quotes around each month. Currently what gets returned is this:
Aug-22, Sep-22, Oct-22, Nov-22, Dec-22, Jan-23, Feb-23, Mar-23, Apr-23, May-23, Jun-23, Jul-23
However, I need it to have quotes around each date as well (with the comma outside) like this:
‘Aug-22′,’Sep-22′,’Oct-22′,’Nov-22′,’Dec-22′,’Jan-23′,’Feb-23′,’Mar-23′,’Apr-23′,’May-23′,’Jun-23′,’Jul-23’
You must be logged in to reply to this topic.
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.