Wordpress loop - how to count items
Here's one way to go about it:
<?php
$count = 0; //set up counter variable
while (have_posts()) : the_post();
$count++; //increment the variable by 1 each time the loop executes
if ($count<4) {
// here put the special code for first three
}
// here put the code for normal posts
endwhile;
?>
You can use the post_count
property of $WP_Query
, like so:
$wp_query->post_count
Be aware of the difference with found_posts
, which counts the posts which, though matching the query, are not being displayed (e.g. for pagination). You might want to use one or the other depending on your particular situation.