How to Place an Ad after the First Post in WordPress

Posted on Mar 25, 2010 in AdsenseAdvertising
This guide will cover up the basic code used to place an ad after the first, second or any other post in WordPress.This method is extremely useful if you wish the create some diversity among your ad positions. On regular WordPress blogs, the most common places today are on top, inside the post and below it. Useful if you are trying to avoid banner blindness on your blog.By the way, this works on any file template you have. Be it the homepage or the subpages (categories or any other page template) as long as there is a post loop. Below you can see an image of how it looks like on the homepage with excerpts and thumbnails in the posts. I used the code there to place an ad unit after the third post (excerpts or full posts, it won’t make any difference in coding).

Ads after posts(click to enlarge – press escape if you can’t close it)

To get an ad shown after an specific amount of posts you first have to go to your index.php file (which is your homepage). Here you need to search for:<?php endwhile; ?>Now place this code directly above it: <?php if ($count == 0) : ?>Copy and paste your ADSENSE CODE here<?php endif; $count++; ?>Note the $count == 0, this is where you can adjust the position of where the ad will be seen. Check out the image below to see which number will omit the post at it’s position. (click to enlarge)ads after amount of postsSo with $count == 0 then the ad will pop up under the first post. With $count == 1 it will pop up under the second post. $count == 2 will show it under the third post. Etc….Easy isn’t it? If you have any questions regarding this, do not hesitate to ask them.Cheers,Mark