views:

115

answers:

2

Hey,

What i want to do i quite simple : 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?

Cheers,

Kartik Rao

A: 

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

while (have_posts()): the_post();

if ($wp_query->current_post % 3):

    // your ad code here

endif;

// your normal post code

endwhile;

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

TheDeadMedic
I tried this method. The ad is being inserted before and after every 3 posts! How do i get it to insert the ad only after 3 posts.
Kartik Rao
A: 

Why not incrementing a variable then display your ads when needed?

while(LOOP)
    echo $i%3==0 ? $ad : '';
    $i++
fabrik