Google Adsense logo

How to insert Ads between posts in your WordPress homepage site

Google Adsense logoFor some time now, surfing around the internet I’ve noticed, and you may have noticed as well, that a lot websites at the homepage have ads between posts from Google Adsense and other advertising companies. One of the main reasons is that one of the most effective places to insert ads is right-after the first post and it could potentially increase revenue. But you should notice that there may be a difference of CTR (Click Through Rate) when you compare to ads inserted before the first post, but it is like, a mentioned before, more effective and also looks cleaner.

Alright! Now to the good stuff, in this Set WordPress tutorial you are going to learn how insert ads between post in your WordPress homepage:

1.  Connect to your WordPress site via FTP or go to your WordPress Dashboard, in the menu go to Appearance and choose Editor. Now locate and open the index.php or the loop.php (this will depend on your theme template) file and look for a line of code similar to this:

<?php while (have_posts()) : the_post(); ?>

2.  Once you’ve found it, you need to insert the following line above it:

<?php $count = 1; ?>

3.  Next find the following line that looks like this:

<?php endwhile; ?>

4. Once you’ve found it, you need to insert the following lines of code with the Google Adsense code or the code from the advertising company you are using above it:

<?php if ($count == 1) : ?>
YOUR GOOGLE ADSENSE CODE HERE
<?php endif; $count++; ?>

Now if you see in the last piece of code, the first line contain this part: $count ==1, the number 1 indicates the position where the ads will be inserted, in this case will be after the first post. If you want to insert ads after the third, fourth post… You are going to have to increase this number to 3 or 4 and so on.

Bonus

What about adding more than one ad unit between posts in the homepage!

Well if this is your case, you need to follow all the steps above and insert the following lines of code right-after php endif; $count++; ?>:

<?php if ($count == 3) : ?>
YOUR GOOGLE ADSENSE CODE HERE
<?php endif; $count++; ?>

As you can see the code is just part of the first ad, only this time we increased the number or the position of where the next ad will be inserted — Play with the numbers until you get the results you want –.

Remember to save the index.php or the loop.php file, and now your WordPress site at the homepage is going to display an ads between the published posts, depending in your configuration. Happy revenue!

Tip: You can use this same method to insert other things, such as  images, customs ads, etc. by just replacing the Google Adsense code with what you want.

If this Set WordPress tutorial was useful to you, please share it in Twitter, Facebook and Google +1.

11 thoughts on “How to insert Ads between posts in your WordPress homepage site

  1. My index loop is structured as such:

    It only has on opening and closing tag … How can I implement the above code to work with this kind of loop structure?

  2. thanks sir. it worked perfectly. i tried many codes but yours was easy to understand. wish you all the best. keep posting,

  3. Hi there,
    Nice post. But I can’t get it working with multiple blocks. I have this now:

    YOUR GOOGLE ADSENSE CODE HERE 001

    YOUR GOOGLE ADSENSE CODE HERE 002

    YOUR GOOGLE ADSENSE CODE HERE 003

    It only shows me the first block. Not the rest.

    I guess I do something wrong….

  4. to add more then one ad between posts… you have to do it like this:

    YOUR GOOGLE ADSENSE CODE HERE
    <?

    YOUR GOOGLE ADSENSE CODE HERE

    YOUR GOOGLE ADSENSE CODE HERE

    and so on…

    Have to use ” elseif ” as seen above…

Leave a Reply