X

How to Add Google Ads After First Post in WordPress

Snippets by IsItWP

Are you looking for a way to display Google ads after first post in WordPress? While there’s probably a plugin for this, we have created a quick code snippet that you can use to include ad.php file and display Google ads after after the first post in WordPress.

Make sure to create a new file named ad.php and add your Google adsense code in it.

Instructions:

All you have to do is add this code to your theme’s single.php file or in a site-specific plugin:

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

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly add code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: 15 best content marketing tools and how to create a custom user registration form in WordPress.

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!