PSA:高级自定义字段the_repeater_field经常迭代

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

对于那些有相同问题的人,这不是一个问题,而是一个答案。

我对ACF中的the_repeater_field()函数有问题。我曾经有一个带有自定义字段转发器的自定义帖子,客户可以在其中输入一些演示文稿的日期等。

现在,他想为不同的服务提供相同的服务,因此我为每种服务创建了一些新的约会帖子。突然所有日期将重复3次。

所以问题出在while(has_posts());为我的自定义帖子类型。显然,它遍历了该自定义帖子类型的每个帖子,然后调用了Repeater字段函数。

这里是错误代码供参考://事先获取自定义帖子//事先取出客户信息,并用占位符替换所有内容

if($getposts->have_posts())
{
     while(has_posts()){ //<--- this is wrong
        $getposts -> the_post();
        $theID = get_page_by_title( 'pagetitle', '', 'customposttype' );

        //$type is the type of date, could be online or in person

        if(get_field($type, $theID->ID)){
            while(the_repeater_field($type, $theID->ID)){

                $date = get_sub_field('date');
                $time = get_sub_field('time');
                $header = get_sub_field('header');
                $email = get_sub_field('email_text');


                if($today <= $date)
                {


                    $return .= "<a href='mailto:[email protected]?subject=".$header."&body=".$email."'>".$date." - ".$time. " Uhr </a></br>";
                }
            }
          } 
       }
}

希望我能帮助可能遇到类似问题的人。找不到任何在线内容。虽然在那儿有while循环是一个菜鸟的错误。几个月没有造成伤害,因为它仅用于一项服务。

php wordpress advanced-custom-fields
1个回答
0
投票

这里是工作代码:

if($getposts->have_posts())
  {
    $getposts -> the_post();
    $theID = get_page_by_title( 'pagetitle', '', 'customposttype' );

    //$type is the type of date, could be online or in person

    if(get_field($type, $theID->ID)){
        while(the_repeater_field($type, $theID->ID)){

            $date = get_sub_field('date');
            $time = get_sub_field('time');
            $header = get_sub_field('header');
            $email = get_sub_field('email_text');


            if($today <= $date)
            {


                $return .= "<a href='mailto:[email protected]?subject=".$header."&body=".$email."'>".$date." - ".$time. " Uhr </a></br>";
            }
        }
      } 
   }
© www.soinside.com 2019 - 2024. All rights reserved.