X

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.



Code Snippet:


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

Comments  Leave a Reply

  1. 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.

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

    2. 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

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

        1. no problem glad that I could help!

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!