Is there a way to get a number of items within Wordpress loop code:
<?php while (have_posts()) : the_post(); ?>
This loop lists the posts. I need to add certain classes to first 3 depending on the total number of them.
Is there a way to get a number of items within Wordpress loop code:
<?php while (have_posts()) : the_post(); ?>
This loop lists the posts. I need to add certain classes to first 3 depending on the total number of them.
I used this in mine
You can use the
post_count
property of$WP_Query
, like so: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.Here's one way to go about it: