How best to inject ads between posts in the loop?

I’m working on my first ever WP project.

I have a theme that tiles all posts across the homepage – and I need to add a repeating advert every 5-6 posts.

My thought was to change the database query that lists the posts and add the advert every so many loops.

Can someone point me in the direction of where to find the database query?

Or is there a more elegant solution you could suggest?

thank you.

2 Answers
2

Probably just add a counter, show ad on multiples of 6.

Something like

$count = 0;
$adEvery = 6;

if (have_posts()) :
    while (have_posts()) : the_post();

        // Individual Post

        $count++;
        if ($count%$adEvery == 0) { 
            // your ad
        } 
    endwhile;
else :
    // No Posts Found
endif;

Leave a Comment