Inserting ads into Jetpack infinite scroll loop

97 views Asked by At

I am creating an infinite scrolling blog and would like ads to be shown every 5 posts. My code is as shown:

        <?php 
            while (have_posts()) : the_post();

                if ($i < 4) {
                    get_template_part( 'content', get_post_format() );
                    $i++;
                } elseif ($i == 4) {
                    get_template_part( 'ad_template', get_post_format() );  
                    $i=0;
                }

            endwhile; 
        ?>

This works fine until Jetpack's infinite scrolling takes over and no longer shows the ads at all. I'm wondering if there is a way I can continue to show ads even though after 7 posts Jetpack takes the reigns.

1

There are 1 answers

0
Kyon147 On

Instead of having two different template parts, try adding that into the 'content' template.

Like so:

if ($i < 4) {
    // Your actual post //
    $i++;
} if($i == 5) {
 // your ad code //    
    $i=0;
}

That way it should be a part the infinite scroll output. I have not tested this though so can not be 100%