Thanks for your support!
Tutorial: How To Add AdSense Block Above Your First Post
Have you ever wanted to add an AdSense ad block above your first post? Or maybe you have been trying to figure out how to display an ad block before your first three posts?
That was the question that a reader recently asked:
Since I have seen this question come up in a few other places as well, I thought it would be helpful to explain how you can do this on your own WordPress blog. You may see some slight differences on your own site depending on the theme that you are using but the general idea will be the same.
Quick and Dirty Method
The following steps are a little crude but will get the job done. After making this minor code change, you will have an AdSense block displayed above your first post.
Step One
The first thing to do is navigate to your Theme Editor (Design -> Theme Editor) and select the “Main Index Template (index.php)” page.
Step Two
In order to add an AdSense block directly above the very first post, you will want to find the line of code that opens the DIV block used to display each post. There will most likely be a “while” or “foreach” statement as well, it should look something like the following:
<?php while (have_posts()) : the_post(); ?>
<div class=”post” id=”post-<?php the_ID(); ?>“>Step Three
Directly above these lines of code, you will want to insert your AdSense code. If you were to place your ad code after the “while” or “foreach” loop, you would end up displaying the ad block before every post on the page - which would exceed the maximum number of ad blocks permitted.
To make this step a little easier, I recommend using the AdSense Deluxe plugin to define the variety of AdSense blocks that you will be using. Then inserting the ad code would look something like this:
<?php adsense_deluxe_ads(‘468×60′); ?>
If you’re not using the plugin, you would simply insert the standard AdSense code block in the same location above the code located in Step Two.
Step Four
Save your file and launch the main page of your blog. You should now see an AdSense block directly above your first post.
Enjoy!
Just As Quick But Slightly Less Dirty Method
As I mentioned earlier, the above method is a little crude and does not allow for much flexibility as it only adds an ad block above the first post.
What about if you want to display an AdSense block before each of the first three posts? Or two posts?
The following method will add a little bit more flexibility as you will easily be able to change the number of AdSense blocks that are displayed by changing one small thing as opposed to adding/removing an entire section of code.
Step One
The first thing to do is navigate to your Theme Editor (Design -> Theme Editor) and select the “Main Index Template (index.php)” page.
Step Two
In the previous method, we looked for the DIV tag and placed the ad code outside of the DIV and the while / foreach loop. This time we will place the code inside the loop but add a little extra code to prevent the ad block from displaying too many times.
Find the code that looks similar to the following:
<?php while (have_posts()) : the_post(); ?>
<div class=”post” id=”post-<?php the_ID(); ?>“>Step Three
Now we are going to add the AdSense block again, although this time we are also going to add some PHP code that will be used as a counter. The counter will display the ad block until it exceeds the number that we specify and then will no longer render the ad block.
This might sound daunting but it is pretty easy. Our code will look something like this when we are done:
<?php $counter = 0; ?>
<?php while (have_posts()) : the_post(); ?>
<div class=”post” id=”post-<?php the_ID(); ?>“>
<?php if($counter < 3) { adsense_deluxe_ads(‘468×60′); } ?>
<?php $counter++; ?>The code above will display an ad block before the first three posts on the page. If you only want to show the ad before the first post, change the “3″ above to a “1″ and you are done. Later if you decide you want an ad block before the first two posts - no problem, just change the “1″ to a “2″ and you’re done.
Step Four
Save your file and launch the main page of your blog. You should now see an AdSense block directly above the first three - or whatever number you used - posts.
Enjoy!
Hopefully you were able to follow along with this brief tutorial on adding an AdSense block above your post and can now sit back and wait for the money to start rolling into your account - if only it were that easy.
Should you run into any problems or have any questions, please leave a comment below and I will do my best to help you out.





























You should also note that using this method will only get the Adsense block to show up on your main index page. To get the same Adsense block within individual blog posts, you’ll need to edit the single.php file as well. For site-wide ads, things get even more messy, because you have to edit the templates for Wordpress pages, 404, search results, and so on.
Michael Kwans last blog post..Demystifying the Work From Home Phenomenon
Correct - the above method will address the main page of your blog that traditionally displays 5-10 posts. Thank you for pointing that out.
Having written some php and putting some quick-and-dirty hacks into my WordPress page, I read this post and thought “oh, of course, that’s straight-forward”. On the other hand, I’m more curious about how you did all the syntax highlighting when you quote the code…
Kelvin Kaos last blog post..How to make a moving mouth sheep puppet (Bobby)
Take a look at the following PHP function: highlight-string
Oh wow, what ISN’T built into PHP?
Sorry, but I stopped reading somewhere around Step 2. BTW, why don’t you use Adsense on this blog?
At one time I did have AdSense displayed on this blog. However, with an audience that is more involved with blogging I found that the ads were not converting at all so I removed them.
There are a few posts here and there that have ads on them though, feel free to poke around looking for them.
That’s one problem I have found with blogging is that while it is easy to network and receive readership and encouragement from other bloggers, having that sort of audience makes it a bit tougher to sell advertising and get adsense clicks that it probably would be with a more “uneducated” audience.
I agree that AdSense would not be the best choice for this site, besides, you make enough each month that the $2.00 a month you’re missing out on isn’t much of a loss for keeping your site cleaner.
Nicks last blog post..8 Random Facts About Me
Derek,
Thank you so much for this tutorial. I have wanted to learn how to do this so many times. When I am setting up a niche blog, sometimes I don’t want to have to become an expert with a particular WordPress Theme. This tutorial allows me to quickly get my adsense ads where I want them.
Thanks!!
Wachovia Mortgage Ratess last blog post..Wachovia Mortgages Helped by GoldMan Sachs?
Great, glad you liked it and I hope you find it useful.
Best solution yet. Get a knowledgeable webmaster to help you! That is what I would do.
ya know, i actually thought about doing this at one time…i figured i would give it a shot before i do.
Kudos to you for trying, most people would take one look at the code and give up…
PHP really isn’t too tough once you start looking at it, it’s actually fairly “human readable”
Not to steal Derek’s thunder and advertise on his blog, but: Nicole and Andrea, it would probably be fairly easy to “bribe” me to help you with stuff like this as well.
Nicks last blog post..8 Random Facts About Me
bribes, bribes…..Yeah I guess I could bribe you but I am using iWeb08 to build my website. I am fairly new to it and I have been looking for help with it but it seems others dont know it very well either. I am slowly getting the hang of it though. It is pretty user friendly but damn, its going slow!
I just set up an adsense acct and now i dont know wtf to do with it! LOL
I’ve never even heard of iWeb08
Nicks last blog post..Movie Review - Wanted
Nicole and Andrea, you can always hire me!
I for one doubt that I can afford you. I did not say, hire Derek. I just have to bat my eyelids and say pretty please!
thank you derek!
Thanks Derek for the info!
There is a plugin that does that easily also, and I find it better than Adsense Deluxe. It is called Shylock AdSense.
Im just starting with adsense:) Thanks for the tip.
Thanks for the tips, Derek.
dianis last blog post..Affiliate Marketing What is This?
Will this work on blogger and wordpress?
I believe it will as I thought that you can now have AdSense integrated on your blogger account. When you mention WordPress, I assume you are asking about a WordPress.com hosted account and that I am not sure if they allow you to integrate AdSense or not - I haven’t kept up with what they allow you to do.
If you can modify your template files and using AdSense complies with the terms of use, then you should be able to do this as well.
Lol, when I mention Wordpress, I am not sure what I am talking about to be honest. I have a blogger account and a standard html site, that is my limit!
No problem - there are two “flavors” of WordPress, one being the WordPress.com hosted accounts and the other being WordPress.org where you have your own domain and hosting account.
Obviously when you host the account yourself, you are free to do as you wish because you have control of everything. Traditionally the Blogger/Wordpress hosted accounts have been a little more restricted in what you can do, such as installing plugins, themes, etc.
In response to upspot, I think Derek is wise not to have adsense on this site. It is not a site which would trigger good ads and probably would not get many clicks because of the type of people that come here.
Laser Dentists last blog post..Minneapolis laser dentist
Derek this is a great tutorial, it would be worth noting, that knowing a little bit of php can go a long ways when tweaking WordPress themes. I used a similar method to put the ads after my posts on my main page.
In my opinion, it’s generally better to directly modify your theme and template files rather than add an additional plugin or widget.
Nicks last blog post..8 Random Facts About Me
Why doesn’t wordpress offer this in the theme layout? I’m sure it will a be popular feature.
Eva Whites last blog post..First Pregnant Man
Great guide. I plan on implementing Adsense on my blog, and this saved me a lot of time.
Johns last blog post..McCain: Obama’s Word `Cannot Be Trusted’
When I used to run a personal website, I had AdSense in between my posts and it worked pretty well. For my blog, I wouldn’t consider putting AdSense on, but you gave me a great idea to put on a blog on my old site! That would make things really efficient and create another stream of income for me. Thanks for the inspiration, Derek (I just thought of this now, I’m really happy about it, ha).
Does adsense have any bearing or affect at all on SERPS? Anything you need to watch out for particularly?
Not in my experience - nothing you really need to watch out for other than the fact that you shouldn’t click on your own ads.
Thanks for the info.I definitely got some questions answered.
Hi Derek,
Thanks for the walkthrough… this type of work is about the most complex coding (if you will) that I’m capable of.
Rick NHSs last blog post..Home Inspection Checklist
Good post and good tip but why not just use the Adsense all in one plugin.
David Hobsons last blog post..104 Social Bookmarking Websites to Submit Too
Hmm, this is quite interesting. Maybe I’ll be setting up some ad’s in my posts.
As a poster on blogs, if adsense is before the first post, I will invariably close the tab as the objective should be visitors then income.
this can be done with admanager plugin too…
latest gadgetss last blog post..Free Wordpress theme
This is very helpful article specially for those only starting.
Guess what I think blogger read your post. Now they seem to be offering something similar.
Eva Whites last blog post..Herbal Viagra - A New Discovery
1 Trackback / Pingback