【问题标题】:How do you continue an action after you add a break in a PHP Loop?在 PHP 循环中添加中断后如何继续操作?
【发布时间】:2017-06-14 19:01:36
【问题描述】:

所以这就是问题所在。我有两个 PHP 文件(one.php 和 two.php)。第一个循环从 0 开始,到 77 结束。

下一个循环从第 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 模板文件中:

【问题讨论】:

  • 也许你不需要continue
  • @Sysix 好的,你有替代方案或建议吗?
  • 告诉我应该输出哪些行?使用$i 变量作为计数器;)
  • 我不明白为什么第一个循环应该在 77 之后停止 - 它不应该做任何有趣的事情,因为在第 8 行周围有 continue。第二个应该从 79 运行到 100,因为休息。第二件事 - 你想达到什么目的?
  • 那么,如果你想要77,为什么还有if( $i &gt; 40 )?为什么第 8 行有continue - 你怎么期望第 9 行这样一个被执行?

标签: php wordpress advanced-custom-fields


【解决方案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;
?>

【讨论】:

  • 漂亮!像魅力一样工作
  • 两种解决方案中的哪一个?所以我可以编辑我的答案并帮助其他人。
  • 第一个答案似乎对我有用。我刚刚测试过
猜你喜欢
  • 2012-10-18
  • 2019-12-26
  • 1970-01-01
  • 2023-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-29
  • 2010-09-05
相关资源
最近更新 更多