如何将转发器子字段变成标签

How to turn repeater subfield into tag

我有一个循环,它通过转发器以天的格式输出日期和时间子字段的不同值 ('d')。我想知道如何将 $dia 的每个不同值变成一个标签,以防它有多个值(问题是我到目前为止所做的只输出 $dia 的最后一个值)。

这是我目前拥有的:


if( have_rows('quando', $post_id) ):
 while( have_rows('quando', $post_id) ): the_row();

   $dataCrua = get_sub_field('dia_e_hora', $post_id);
   $data = DateTime::createFromFormat("Y-m-d H:i:s", $dataCrua);
     if ( is_object($data) ) {
       $dia = $data->format('d');
     }

   $tags = $dia;
   wp_set_post_tags( $post_id, $tags);

 endwhile;
endif;
 ?>

如果'dia_e_hora'是一个转发器$dataCrua是一个数组。您将不得不使用 foreach 遍历它!

我认为这应该可行!?

<?php
if( have_rows('quando', $post_id) ):
    while( have_rows('quando', $post_id) ): the_row();

        $dataCrua = get_sub_field('dia_e_hora', $post_id);
        $dia = array();
        foreach($dataCrua as $data_entry) {
            $data = DateTime::createFromFormat("Y-m-d H:i:s", $data_entry);
            if ( is_object($data) ) {
                $dia[] = $data->format('d');
            }
        }

        $tags = $dia;
        wp_set_post_tags($post_id, $tags);

    endwhile;
endif;
?>

解决了!

    $tags = array();
    if( have_rows('quando', $post_id) ):
      while( have_rows('quando', $post_id) ): the_row();

    $dataCrua = get_sub_field('dia_e_hora', $post_id);
            $data = DateTime::createFromFormat("Y-m-d H:i:s", $dataCrua);
      if ( is_object($data) ) {
              $dia = $data->format('d');
            }

            $tags[] = $dia;
            wp_set_post_tags( $post_id, $tags, false);

        endwhile;
    endif;
     ?>