如何将转发器子字段转换为标签

问题描述 投票:0回答:2

我有一个循环,该循环通过中继器以日期('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;
 ?>
wordpress loops tags repeater
2个回答
0
投票

如果'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;
?>

0
投票

解决了!

    $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;
     ?>
© www.soinside.com 2019 - 2024. All rights reserved.