如何在php中运行while循环最多5次

时间:2017-06-12 09:35:49

标签: php while-loop

我总共有10个帖子,但我想循环播放只有5次。并且只想显示5个值。我是wordpress / php的新手,所以任何一种帮助都会非常感激:)

这是我的代码:

<?php query_posts('page_id=4');
                    $i=0;
                    ?> 
                   <?php while (have_posts()) : the_post(); 

if( have_rows('numbers_area') ): while ( have_rows('numbers_area') ) : the_row();
                if( get_row_layout() == 'numbers_area' ):

                    ?> 

                    <div class="col-md-2 <?php if ($i==0){ ?> col-md-offset-1 <?php } ?> col-sm-6">
                        <div class="facts_information facts_information_1">
                            <h5><span class="count"><?php echo get_sub_field('numbers'); ?></span></h5>
                            <p><?php echo get_sub_field('details'); ?></p>
                            <i class="<?php echo get_sub_field('icon'); ?>" aria-hidden="true"></i>
                        </div>
                    </div>

                     <?php

                    $i++;

              endif;
          endwhile;
        endif;
          endwhile;
        ?>

2 个答案:

答案 0 :(得分:0)

<?php while (have_posts()) : the_post();
if($i>=5){break;}
//other code

这项工作肯定是..

答案 1 :(得分:0)

<?php 
  query_posts('page_id=4'); 
  $count = 0; 
  while (have_posts()):
      the_post();  
      $count=$count+1;
      if(count == 5 )
          break;
      endif;
  endwhile;
?>