2017-06-14 39 views
-1

所以問題在這裏。我有兩個PHP文件(one.php和two.php)。第一個循環從0開始並停在77.在PHP循環中添加中斷後,您如何繼續操作?

下一個循環從行78開始,停在300.這部分工作。出於某種原因,來自two.php的所有行都不顯示。我認爲one.php的循環阻止了two.php完全運行。我在WordPress中使用高級自定義字段(ACF)。

/*** One.php ***/ 
<?php 
    if(have_rows('repeat_field')): 
    $i = 0; 
    // loop through the rows of data 
     while (have_rows('repeat_field')) : the_row(); 
     $i++; 

      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 > 77) 
     { 
      break; 
     } 
     endwhile; 
    else : 
     // no rows found 
    endif; 
?> 


/*** Two.php ***/ 
<?php 
    if(have_rows('repeat_field')): 
    $i = 0; 
    // loop through the rows of data 
     while (have_rows('repeat_field')) : the_row(); 
     $i++; 
     if($i<79) 
      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 > 100) 
     { 
      break; 
     } 


     endwhile; 
    else : 
     // no rows found 
    endif; 
?> 

兩個文件都包含在一個PHP模板文件:

+0

也許你不需要'continue' – Sysix

+0

@ Sysix好了,你有沒有替代方案或建議? – Mariton

+0

告訴我應該輸出哪些行?使用'$ i'變量作爲計數器;) – Sysix

回答

1

have_rows您實習生循環中,您可以輕鬆地繼續循環的行。

這裏是代碼:

/*** One.php ***/ 
<?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 >= 77) 
     { 
      break; 
     } 
     endwhile; 
    else : 
     // no rows found 
    endif; 
?> 


/*** Two.php ***/ 
<?php 
    if(have_rows('repeat_field')): 
    // 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 >= 100) 
     { 
      break; 
     } 


     endwhile; 
    else : 
     // no rows found 
    endif; 
?> 
+0

美麗!工作像一個魅力 – Mariton

+0

這兩種解決方案?所以我可以編輯我的答案並幫助其他人。 – Sysix

+0

第一個答案似乎適用於我。我剛剛測試了 – Mariton