有没有办法使用 Wordpress 高级自定义字段 (ACF) 从特定行开始?

Is there a way to start on a specific row using Wordpress Advanced Custom Fields (ACF)?

所以我在 Wordpress 中使用 ACF(高级自定义字段)。我有大约 40 行要显示,但我想从第 5 行开始。基本上我想显示第 5 行到 40 行,而不是从第一行开始。有没有办法做到这一点?我的代码如下。

          <?php




          if( have_rows('repeat_field') ):
          $i = 0;

            // loop through the rows of data
              while ( have_rows('repeat_field') ) : the_row();

              $i++;


            if (!empty(get_sub_field('feature_image_post'))) {

                the_sub_field('feature_article_link'); 
                the_sub_field('feature_image_post'); 
                the_sub_field('feature_title'); 

            } 

              if( $i > 40 )
              {
                break;
              }


              endwhile;

          else :

              // no rows found

          endif;

          ?>

您应该可以使用如下代码实现此目的:

<?php

$repeat = get_field('repeat_field');

for ($i = 5; $i <= 40; $i++) {

    if (!empty($repeat[$i]['feature_image_post'])) {

        echo $repeat[$i]['feature_article_link']; 
        echo $repeat[$i]['feature_image_post']; 
        echo $repeat[$i]['feature_title'];         

    } 

}

?>

只需使用get_field获取中继器的数组,然后使用PHP中的for函数循环遍历第5到第40

您应该在循环中使用 continue 语句来跳过迭代,如下所示:

<?php
      if( have_rows('repeat_field') ):
      $i = 0;
        // loop through the rows of data
          while ( have_rows('repeat_field') ) : the_row();
          $i++;
          if($i<5)
              continue;
          if (!empty(get_sub_field('feature_image_post'))) 
          {
              the_sub_field('feature_article_link'); 
              the_sub_field('feature_image_post'); 
              the_sub_field('feature_title'); 
          } 
          if( $i > 40 )
          {
              break;
          }
          endwhile;
      else :
          // no rows found
      endif;
?>