Dipankar Das Dipankar Das -4 years ago 47
PHP Question

How to run while loop maximum 5 times in php

I have total 10 post but i want to run while loop only 5 times. and want to show only 5 values. I am very new in wordpress/php so any kind of help would be really appreciated :)

Here is my code :

<?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;
?>

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

this work for sure..

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download