Home / loop / Google ads after first post
  • How to Google ads after first post


    This is an example of a loop that will use the loopcounter variable and include ad.php file after the first post. If you add your Google adsense code within the ads.php file or anything else it wil be displayed after the first post.

    ( click code to copy )

    wordpress snippet : PHP

    <?php if (have_posts()) : ?>
        <?php while (have_posts()) : the_post(); $loopcounter++; ?>
            // the loop stuffs
           <?php if ($loopcounter <= 1) { include (TEMPLATEPATH . '/ad.php'); } ?>
        <?php endwhile; ?>
    <?php else : ?>
    <?php endif; ?>

    ( WordPress codex functions, hooks, in this snippet. )

    template, have_posts, the_post,
  • Any idea how to do this with Twenty Eleven theme? It was easy in Twenty Ten, but I’ve tried several of them in Twenty Eleven but the loop is different and it ends up displaying several times.

    • Hi Zeaks ill take a look at the theme and let you know.

    • It is almost the same as the above code, I put the code within the main index.php template. I put the code in pastebin.com so you can take a look. http://pastebin.com/naVwL9Nb

      • Thanks, for some reason I was thinking this had to be added to content.php, it worked fine once I added it to index