2017-06-06 61 views
0

所以我使用ACF(高級自定義字段),我想顯示前四行。雖然我在所有20行,我只需要在第一個五年行或最後5行如何使用PHP在前4行顯示字段?

而且,我的代碼並不需要在一個循環中顯示的字段,所以我願意接受任何答案。我只把我的代碼放在一個循環中,因爲這是我知道如何顯示中繼器子字段的唯一方法。我非常新的使用領域ACF

<?php 

    // check if the repeater field has rows of data 

    if(have_rows('repeat_field')): 

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

      // display a sub field value 

       the_sub_field('place_name'); 
       the_sub_field('place_date'); 
       the_sub_field('place_time'); 


     endwhile; 

    else : 

     // no rows found 

    endif; 

回答

0

只要停止整理運行的次數後的循環,你會想:

if(have_rows('repeat_field')): 

    $i=0; // Counter to keep track of number of runs 

    while (have_rows('repeat_field')) : the_row(); 
      the_sub_field('place_name'); 
      the_sub_field('place_date'); 
      the_sub_field('place_time'); 

      $i++; // Increase the number by one after each run 
      if($i==4) break; // Stop after four runs 

    endwhile; 
endif;