Loop with conditional counting

This loop is commented well so just have a look at the code:

<?php if (have_posts()) : 
	// if there are posts to display, set the count to zero and begin the loop
	// if there are posts to display, process any code included here only once
	// display any code output from this region above the entire set of posts
	?>
  <?php $count = 0; ?>
  
	<?php while (have_posts()) : the_post(); 
		// for each post, increase the count variable by one and process any code included here
		// the output of any code included here will be displayed above the content of every post
		?>
    <?php $count++; ?>
    
    
    <?php if ($count == 1) :
      // == 	is equal to the $count variable
      // != 	is not equal to the $count variable
      // > 	is greater than the $count variable
      // >= 	is greater than or equal to the $count variable
      // < 	is less than the $count variable
      // <= 	is less than or equal to the $count variable

      // if this is the first post, process any code that is specified in this region
      // process any code specified in this region before the content of the first post
      ?>

    <?php else : 
      // if this is not the first post, process any code specified in this region
      // process any code specified in this region before the content of each post
      ?>

		<?php endif; 
  		// for each post, including the first, process any code included here
  		// any code output will be displayed below the content of every post
  		?>

	<?php endwhile; 
  	// stop the post loop and process any code included here only once
  	// any code output will be displayed below the entire set of posts
  	?>

	
<?php else : 
	// if there are no posts to display, process any code that is included here
	// the output of any code included here will be displayed instead of posts
	?>
	<p>Sorry, no posts matched your criteria.</p>

<?php endif; ?>

Revisions

There are no revisions for this post.

Tags: , , ,

No comments yet.

Leave a Reply