WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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'); ?>
<?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

share|improve this question
How are the new posts on the infinite scroll page added? The code you've posted probably doesn't effect posts that are added dynamically later, so you'll need to add something in the loading function to include the advert. – Simon Forster Sep 5 '12 at 11:36
May the flying spaghetti monster bless AdBlock :) – ThiefMaster Sep 5 '12 at 11:42
Thanks for the response, how would I go about doing that ? I am using github.com/webcreate/Infinite-Ajax-Scroll in place of infinite scroll. Most of the documentation is in there. – espnicholas Sep 5 '12 at 11:56
I've replied in the form of an 'answer'. – Simon Forster Sep 5 '12 at 12:17
Thanks, I'll look into see if I can solve a solution in the mean time. – espnicholas Sep 5 '12 at 12:26

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


Then in your custom js,

ias.on('rendered', function(items) {
    var $items = $(items);
        '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.

share|improve this answer

The solution to your problem is to add functionality using the onLoadItems event, whereby you inject an advert every time ten more items have been added, keeping track of the number of items loaded by adding the size of the items array passed when the event is triggered to a counter of some sort and when the counter hits 10, adding an advert and resetting your counter to 0.

From your response I'm assuming your level of JavaScript knowledge isn't that great? And also that you'd struggle to do this change even with the documentation provided with the script? Am I correct? Do you know what events are and how they work? If not, you could wait for someone to post a fully coded solution here, or raise your question as an issue on the GitHub issue tracker.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.