Have you ever wondered how to show Google ads on your WordPress blog’s home page, but only after the very first article? Well, here is how you do it. And don’t worry, it’s quite simple, and you can’t permanently damage anything by giving it a try. ANYONE can do this!
- In the PRESENTATION menu, under THEMES EDITOR, find the theme file called “Main Index Template” (index.php).
- FIRST: Copy and paste everything you see here into a blank text file and save it. This way if you screw something up you can revert back to the original without hurting anything.
- Find a line of code that looks much like the following:
<?php the_content('Read the rest of this entry »'); ?>
- Immediately after that, paste the following:
<?php if ($count == 0) : ?> ENTER YOUR AD CODE HERE <?php endif; $count++; ?>
- Save the document and refresh your page. You should see the ad ONLY after the first article on the homepage.
So as an example, here is what the code might look like when installed (the red part is the new part):
<div class=”Post”>
<div class=”PostHead”>
<h1 class=”title”><?php the_title(); ?></h1>
<p class=”PostInfo”>Posted on <?php the_time(‘M d, Y – g:ia’); ?> <?php comment_time() ?> by <?php the_author() ?> in <?php the_category(‘, ‘) ?></p>
</div>
<div class=”PostContent”>
<?php the_content(‘Read the rest of this entry »’); ?>
</div>
</div>
<?php if ($count == 0) : ?>
<div class=”frontpage-google-ad” style=”border: dashed 1px black; padding: 10px” align=”center”>
<script type=”text/javascript”><!–
google_ad_client = “pub-0617773482327857”;
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = “468x60_as”;
google_ad_type = “text_image”;
//–>
</script>
<script type=”text/javascript” src=”http://pagead2.googlesyndication.com/pagead/show_ads.js”></script>
</div>
<?php endif; $count++; ?>
If it doesn’t show up in the right place, just try moving it around a bit and use a little trial and error. Also, remember to disable WP-Cache while you are testing the changes. If your pages are cached you won’t see the changes and it might throw you off a bit. You can enable it after you are satisfied.
Hey The problem is, I am seeing same ads on individual post also. What to do?
Thankyou but you can use other ways too.
See here.
http://www.bitrepository.com/insert-google-adsense-ads-between-your-wordpress-blog-posts.html
worked great, thanks!
Great advce – I tried it – works perfect! Thank you!!
I have exactly the same question like Shawn. I tried to copy the same code after the first one and only replaced the count number (from 0 to 2), but in that case no ads appear on the screen at all.
What could be the correct code in that case?
That’s what I figured but I tried that and it broke the page.
Shawn,
I’ve not tested this, but I think I would just add a second copy with the other ad in it but change the PHP count to be 1 or 2 or whatever.
So, add another one that starts with:
<?php if ($count == 2) : ?>
John
What would the code look like if we wanted an ad after the first *and* second posts?
Such a small piece of code for a nice big result! Thank you!
I have try your code and its worked !
thanx for nice post Dear :)
ahh ok, thanks john
i usually have adsense ad on my content area, and if i have about 10 post on main page, with 3 max adsense banner, there will be a blank gap on 4th posting … so that’s why i asked
i use adsense deluxe plugin myself but it does not have option to limit how many banners should appear on front page and such
still a good tip
Well, actually, the count ==0 just basically tells the system to place the ad after the first post on the homepage, and before the second one (that is, before the count increments to #1). If you change it to 2, it will put it before the third one, but there will still be only one ad on the page.
So, basically you just need to put the ad code into your theme manually in two other places. If you can hold off on doing it for just a short while, I’m going to work with Donncha to author a comprehensive post regarding who, how, what, when, and why to monetize your blog. I’d guess it will be 1 to 2 weeks max and we’ll tell you what plugins to use, where to put the code, etc.
John
questions: adsense only allow 3 banners, so what if i want to have 3 adsense ads instead of one john?
i increase the “$count == 2”
I just rewrote some of the code in the Adsense Injection plugin… I like that plugin a lot, makes my life easier…
Kim:)
Here you go, this should work:
if ($count == 0) :
if( is_searchengine_user() ) {
// big nasty advert
} elseif( is_regular_user() == false ) {
// smaller less nasty advert
}
endif; $count++;
Ok. So I was going to add the code to my example from this post, but I just realized that I don’t know how!
I don’t think it’s actually possible to nest PHP commands, so could you give me an example of how to add your suggested code here to my code above in the post?
Thanks once again!
John
Adding this plugin is very much on my to-do list. I think it’s critical to ‘thank’ readers by displaying ads very selectively and, if possible, not at all to loyal readers.
That is pretty straight forward. I thought there was much more code required to achieve this.
What I usually do is something like
1. Check if the user came from a search engine
2. If not, then check if they’re not a regular user
3. err, no number 3.
So:
if( is_searchengine_user() ) {
// big nasty advert
} elseif( is_regular_user() == false ) {
// smaller less nasty advert
}
If you do the search engine check you don’t need to check for the age of the post. That’s included in the first check :)
Yes, it helps a little… Here’s a question:
Now does this mean:
A.) IF it is NOT a regular reader, AND IF it is an old post, AND IF it is from a search engine = show ad
B.) IF it is NOT a regular reader, OR IF it is an old post, OR IF it is from a search engine = show ad
C.) Something entirely different! ;-)
By the way… I was reading through the comments on your post (I wish they were threaded :-)) and came across Ozh’s comment about giving his a try because of the UI. I must say, he’s done a fantastic job with the way that works, and it takes the confusion out of setting up the conditions.
The problem with his plugin is that it doesn’t work with WP-Cache, which of course like 99% of the bloggers I know use! However since you have solved that problem, perhaps you guys should merge your two plugins and make it the Donncha / Ozh Everything + The Kitchen Sink Advertising Plugin!
Seriously, if we could combine your functionality with his interface then no one would have any need for another advertising plugin and I could stop hacking the crap out of everything!
John
I really should write another blog post with examples I suppose.
Try this:
if( is_regular_user() == false ) {
// adsense code
}
if( is_searchengine_user() ) {
// show big ugly ads
}
Does that help?
Thanks for the tip John! It’s always tough balancing placement with general use. Don’t want to slap the readers around with the ads, but you also want to give them some exposure. It’s always nice not having to pay too much out of pocket to run a site :)
Damn! I do need to install it! Er, actually just turn it on. :-) You know, I have it partially installed, but I had a question to ask you that got me stuck and I didn’t finish it yet!
For anyone who doesn’t know what we’re talking about here… I bugged Donncha for months to build this plugin which he released about 2-3 weeks ago… and I haven’t yet installed it! :-( The plugin allows you to set things up so that you never show ads to your regular visitors! Yeah! Try it here.
So, I’ve got the plugin installed, my WP-Config and WP-Cache modified and now I just need to add the code around my ads… but I’m confused by the APIs. (Remember I’m a hack at PHP) So, if I use is-regular-user() do I have to put something after it like == true? Or is that the default state? Also, how do I string them all together?
I guess just a few more usage examples would help quite a bit.
John
Go on John, give my friends adverts plugin a plug! :)
That’s a nice way to do it. The question is: is that an optimal place for an ad. It might be, maybe someone can share some data on that approach.