Save value for each ACF repeater field row and pla

2019-07-13 14:03发布

I am using WordPress and Advanced Custom Fields to build a website.

I have an Advanced Custom Field repeater field for an event listing on my website. For each event row, there is a sub field to input a date.

I have attempted to save those sub fields into a $dates array. However, this $dates array is outputting several arrays with only one value with var_dump().

Output of $dates:

array(1) { [0]=> string(20) "June 5, 2018 5:00 pm" }
array(1) { [0]=> string(22) "June 15, 2018 12:00 am" }
array(1) { [0]=> string(22) "July 13, 2018 12:00 am" }
array(1) { [0]=> string(22) "July 13, 2018 12:00 am" }
array(1) { [0]=> string(22) "July 27, 2018 12:00 am" }
array(1) { [0]=> string(24) "August 18, 2018 12:00 am" }

With the code below, I am trying to iterate through a $dates array and convert the values into a $month variable that outputs a month name. The conversion from the date to month name is working, but I need to place these $month values for each repeater row into one $months array.

I tried to create a $months array below and add each $month value to that array. This code outputs separate arrays for each repeater row with only one month value in the array. (Same issue as I have with the $dates array.)

I'm not sure how to accomplish this or if I am looking at this issue the wrong way. Any help would be appreciated!

<?php if (have_rows('events')):

while (have_rows('events')) : the_row();

$dates = array();
$dates[] = get_sub_field('date_time');

foreach ($dates as $date) {
  $timestamp = strtotime($date);
  $month = date('F', $timestamp);
  /* this code below does not work as intended */
  $months = array();
  $months[] = $month;
}

?>

1条回答
唯我独甜
2楼-- · 2019-07-13 14:45

You're resetting the months array at every loop. Simply move the assignation outside of your while loop.

$months = array();

while (have_rows('events')) : the_row();

    foreach ($dates as $date) {
        $timestamp = strtotime($date);
        $month = date('F', $timestamp);
        $months[] = $month;
    }

endwhile;
查看更多
登录 后发表回答