【问题标题】:How to turn repeater subfield into tag如何将转发器子字段变成标签
【发布时间】:2019-12-09 14:12:16
【问题描述】:

我有一个循环,它通过中继器以日期 ('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


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      解决了!

          $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;
           ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-29
        • 1970-01-01
        • 2022-11-18
        • 2019-05-24
        • 2021-01-21
        • 1970-01-01
        相关资源
        最近更新 更多