Tutorial: How To Add AdSense Block Above Your First Post

by derek on June 26, 2008 · 53 comments

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:

I want to place a 468×60 banner on my blog, in between the content and the various tabs (Home, About, Contact). May I know which .php file i should paste the ad code to?

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('468x60'); ?>

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('468x60'); } ?>
<?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.


Stay In The Loop!

Subscribe to the Derek Semmler dot com feed via RSS or Email to receive notifications when new posts are published. Follow the WordPress ninja on Twitter too!

Michael Kwan June 26, 2008 at 11:34 pm

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

derek June 26, 2008 at 11:37 pm

Correct – the above method will address the main page of your blog that traditionally displays 5-10 posts. Thank you for pointing that out.

Kelvin Kao June 27, 2008 at 12:23 am

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)

derek June 27, 2008 at 9:56 am

Take a look at the following PHP function: highlight-string

Kelvin Kao June 27, 2008 at 11:53 am

Oh wow, what ISN’T built into PHP?

Ling June 27, 2008 at 12:36 am

Sorry, but I stopped reading somewhere around Step 2. BTW, why don’t you use Adsense on this blog?

derek June 27, 2008 at 9:58 am

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. ;)

Nick June 29, 2008 at 11:47 pm

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

Wachovia Mortgage Rates June 27, 2008 at 4:50 am

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?

derek June 27, 2008 at 9:58 am

Great, glad you liked it and I hope you find it useful.

Nicole Price June 27, 2008 at 5:00 am

Best solution yet. Get a knowledgeable webmaster to help you! That is what I would do.

Andrea June 27, 2008 at 7:58 am

ya know, i actually thought about doing this at one time…i figured i would give it a shot before i do.

Nick June 29, 2008 at 11:42 pm

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

Andrea June 30, 2008 at 7:29 am

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

Nick June 30, 2008 at 11:48 pm

I’ve never even heard of iWeb08

Nicks last blog post..Movie Review – Wanted

derek June 27, 2008 at 9:59 am

Nicole and Andrea, you can always hire me! ;)

Nicole Price June 30, 2008 at 8:49 am

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!

Motosiklet June 27, 2008 at 6:17 am

thank you derek!

Andrea June 27, 2008 at 7:40 am

Thanks Derek for the info!

Attorneys Catalog June 28, 2008 at 1:32 am

There is a plugin that does that easily also, and I find it better than Adsense Deluxe. It is called Shylock AdSense.

pauletet June 28, 2008 at 6:38 am

Im just starting with adsense:) Thanks for the tip.

diani June 28, 2008 at 12:36 pm

Thanks for the tips, Derek.

dianis last blog post..Affiliate Marketing What is This?

Steve June 29, 2008 at 2:06 pm

Will this work on blogger and wordpress?

derek June 30, 2008 at 11:12 am

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.

Steve June 30, 2008 at 11:30 am

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!

derek June 30, 2008 at 11:39 am

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.

Laser Dentist June 29, 2008 at 7:54 pm

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

Nick June 29, 2008 at 11:39 pm

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

Eva White June 30, 2008 at 5:39 am

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

John June 30, 2008 at 6:20 am

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’

Justin June 30, 2008 at 7:17 am

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

Steve June 30, 2008 at 11:42 am

Does adsense have any bearing or affect at all on SERPS? Anything you need to watch out for particularly?

derek June 30, 2008 at 12:04 pm

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.

Internet Marketing | Steve Renner June 30, 2008 at 2:07 pm

Thanks for the info.I definitely got some questions answered.

Rick NHS June 30, 2008 at 4:44 pm

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

David Hobson June 30, 2008 at 8:39 pm

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

Futon-Matt July 3, 2008 at 5:22 am

Hmm, this is quite interesting. Maybe I’ll be setting up some ad’s in my posts.

Marketing July 3, 2008 at 1:27 pm

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.

latest gadgets July 4, 2008 at 7:28 am

this can be done with admanager plugin too…

latest gadgetss last blog post..Free Wordpress theme

pauletet July 5, 2008 at 11:04 pm

This is very helpful article specially for those only starting.

Eva White July 10, 2008 at 9:27 pm

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

Max October 17, 2008 at 5:34 am

Is it the same technique for adding larger ad blocks?

Vas November 10, 2008 at 2:49 pm

i like the plugin idea. i do not recommend inserting the code directly into post. wordpress messes it up sometime when you update a post

BloggerSource November 21, 2008 at 6:58 pm

I prefer using All in one adsense plugin, very simple.
but this tips is great, more customizable from plugins

sorry for my bad english

BloggerSources last blog post..Blogspot Tutorial : Understanding Blogspot Settings Functions

accord January 5, 2009 at 6:34 am

Great tips, thanks. I usually just use adsense optimized theme. Will try to implement it.

accords last blog post..Mix Your Adsense Profits With Affiliate Marketing

Blog tips August 15, 2009 at 6:29 pm

Can you show me how to put ads in the middle of the post?
.-= Blog tips´s last blog ..Google Adsense in Blog =-.

derek August 15, 2009 at 8:59 pm

To put the ads in the middle of the post, you’ll most likely want to investigate the use of a plugin to display your ads. The one that I’ve used the most is AdSense-Deluxe, which can be used for ads other than AdSense as well.

Dan November 5, 2009 at 4:26 pm

We use AsSense in our SEM campaign and love it. It’s one of the tools in the toolbox. I definitely attribute traffic increases to it.

derek November 5, 2009 at 5:12 pm

Good to hear that it is working well for you. This is a tool that has been removed from my toolbox for reasons unknown to me but apparently known by Google.

jamie January 26, 2010 at 5:25 pm

Hey,
Thanks for this AdSense tutorial. I’ve just started with PHP and WordPress and it is nice to see more and more people making the effort to write posts like these for beginners like me. If you guys wanna take it eve further, I recommend you get started with this PHP book, which has helped me a lot!

PintuKita February 28, 2010 at 4:44 am

I’m newbie in this field, so..thank you for the tips.
.-= PintuKita hopes you will read… Cara Mudah Mendownload Semua Foto Dalam Album Facebook Dengan 1x Klik =-.

Dragos VANA April 12, 2012 at 12:25 pm

Have recently used this on my classifieds website. Thanks

Comments on this entry are closed.

{ 1 trackback }

Previous post:

Next post: