Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress loop : get current post count inside The Loop

When inside The Loop, I want to retrieve the current post count.

For example, after every 3 posts, I want to insert an ad.

So, how do I get the value of the loop count?

like image 944
Kartik Rao Avatar asked Jul 22 '10 11:07

Kartik Rao


1 Answers

You can use the current_post member of the WP_Query object instance to get the current post iteration;

while ( have_posts() ) : the_post();

    // your normal post code

    if ( ( $wp_query->current_post + 1 ) % 3 === 0 ) {

        // your ad code here

    }

endwhile;

Note, if you're using this inside a function, you'll need to globalise $wp_query.

like image 85
TheDeadMedic Avatar answered Sep 20 '22 10:09

TheDeadMedic