The Entrepreneur Network, Perfect Business, is running a competition to win $1000 in business seed money. You do not need to be a member to vote and you can vote once per day. If you vote for me and I win the competition, you will have a chance to win $250 in cash.

Thanks for your support!

Vote for Me!


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:

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(‘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.


del.icio.us Digg Netscape StumbleUpon Technorati


RSS feed| Trackback URI

41 Comments»

Comment by Michael Kwan from Freelance Writing
2008-06-26 23:34:10
MyAvatars 0.2

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

Comment by derek
2008-06-26 23:37:24
MyAvatars 0.2

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

 
 
Comment by Kelvin Kao from Puppet Kaos
2008-06-27 00:23:12
MyAvatars 0.2

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)

Comment by derek
2008-06-27 09:56:37
MyAvatars 0.2

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

Comment by Kelvin Kao from Puppet Kaos
2008-06-27 11:53:18
MyAvatars 0.2

Oh wow, what ISN’T built into PHP?

 
 
 
Comment by Ling from Uptake Subscribed to comments via email
2008-06-27 00:36:43
MyAvatars 0.2

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

Comment by derek
2008-06-27 09:58:07
MyAvatars 0.2

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

Comment by Nick from romandock dot com
2008-06-29 23:47:23
MyAvatars 0.2

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

 
 
 
Comment by Wachovia Mortgage Rates
2008-06-27 04:50:00
MyAvatars 0.2

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?

Comment by derek
2008-06-27 09:58:36
MyAvatars 0.2

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

 
 
Comment by Nicole Price
2008-06-27 05:00:14
MyAvatars 0.2

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

Comment by Andrea
2008-06-27 07:58:23
MyAvatars 0.2

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

Comment by Nick from romandock dot com
2008-06-29 23:42:52
MyAvatars 0.2

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

Comment by Andrea
2008-06-30 07:29:27
MyAvatars 0.2

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

(Comments wont nest below this level)
Comment by Nick from romandock dot com
2008-06-30 23:48:53
MyAvatars 0.2

I’ve never even heard of iWeb08

Nicks last blog post..Movie Review - Wanted

 
 
 
 
Comment by derek
2008-06-27 09:59:14
MyAvatars 0.2

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

Comment by Nicole Price
2008-06-30 08:49:33
MyAvatars 0.2

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!

 
 
 
Comment by Motosiklet
2008-06-27 06:17:12
MyAvatars 0.2

thank you derek!

 
Comment by Andrea
2008-06-27 07:40:53
MyAvatars 0.2

Thanks Derek for the info!

 
Comment by Attorneys Catalog
2008-06-28 01:32:11
MyAvatars 0.2

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

 
Comment by pauletet
2008-06-28 06:38:05
MyAvatars 0.2

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

 
Comment by diani
2008-06-28 12:36:46
MyAvatars 0.2

Thanks for the tips, Derek.

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

 
Comment by Steve from Free iPods Subscribed to comments via email
2008-06-29 14:06:11
MyAvatars 0.2

Will this work on blogger and wordpress?

Comment by derek
2008-06-30 11:12:38
MyAvatars 0.2

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.

Comment by Steve from Free iPods Subscribed to comments via email
2008-06-30 11:30:20
MyAvatars 0.2

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!

Comment by derek
2008-06-30 11:39:39
MyAvatars 0.2

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.

(Comments wont nest below this level)
 
 
 
 
Comment by Laser Dentist
2008-06-29 19:54:02
MyAvatars 0.2

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

 
Comment by Nick from romandock dot com
2008-06-29 23:39:44
MyAvatars 0.2

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

 
Comment by Eva White
2008-06-30 05:39:12
MyAvatars 0.2

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

 
Comment by John
2008-06-30 06:20:20
MyAvatars 0.2

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’

 
Comment by Justin from Airsoft Guns
2008-06-30 07:17:14
MyAvatars 0.2

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

 
Comment by Steve from Free iPods Subscribed to comments via email
2008-06-30 11:42:20
MyAvatars 0.2

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

Comment by derek
2008-06-30 12:04:31
MyAvatars 0.2

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.

 
 
2008-06-30 14:07:45
MyAvatars 0.2

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

 
Comment by Rick NHS from NHS Real Estate Blog
2008-06-30 16:44:16
MyAvatars 0.2

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

 
Comment by David Hobson
2008-06-30 20:39:06
MyAvatars 0.2

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

 
Comment by Futon-Matt
2008-07-03 05:22:18
MyAvatars 0.2

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

 
Comment by Marketing Subscribed to comments via email
2008-07-03 13:27:31
MyAvatars 0.2

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.

 
Comment by latest gadgets
2008-07-04 07:28:46
MyAvatars 0.2

this can be done with admanager plugin too…

latest gadgetss last blog post..Free Wordpress theme

 
Comment by pauletet
2008-07-05 23:04:01
MyAvatars 0.2

This is very helpful article specially for those only starting.

 
Comment by Eva White
2008-07-10 21:27:50
MyAvatars 0.2

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

 
Note: This site uses KeywordLuv. Enter YourName@YourKeywords in the Name field to take advantage. Keyword only entries will have the link removed.

Name (required)
E-mail (required - never shown publicly)
URI
Subscribe to comments via email
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line=""> in your comment.