Placing Ads after every 10th Post with Infinite Scroll

I have to load a couple of ads, some normal html and two are java script. I’ve tried the PHP method of this:

<?php $postnum++; if($postnum%10 == 0) { ?>
 <div id="topset">
  <ul id="top_ad_list">
      <?php dynamic_sidebar('botad'); ?>
  </ul>
 </div> 
<?php } ?>

What occurs is for the first pagination from the infinite scroll it will load the add, on the new loaded pages it will no longer load the ads.

The Infinite Scroll I am using as follows: Infinite Ajax Scroll

2 Answers
2

See this: http://infiniteajaxscroll.com/docs/events.html#rendered

So, place all the ad code in an ajax function (http://codex.wordpress.org/Plugin_API/Action_Reference/wp_ajax_%28action%29)

add_action( 'wp_ajax_your_ad_fn', 'your_ad_fn');
add_action( 'wp_ajax_nopriv_your_ad_fn', 'your_ad_fn');

function your_ad_fn(){
    // your ad code here
    // if sending js, set Content-type header to application/javascript
    // if sending html, text/html
}

https://stackoverflow.com/questions/9664282/difference-between-application-x-javascript-and-text-javascript-content-types

Then in your custom js,

ias.on('rendered', function(items) {
    var $items = $(items);
    jQuery.get(
        'http://yoursite.com/wp-admin/admin-ajax.php', // I've just illustrated, don't write the url directly, do it via wp_localize_script
    ).done( function( data ) ){
         $items.after(data); // append ad after all the loaded posts
    });        
})

This is NOT the exact code, you have to work out some details, yourself.

Leave a Comment