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.






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.
Go on John, give my friends adverts plugin a plug! :)
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
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 :)
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?
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
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 :)
That is pretty straight forward. I thought there was much more code required to achieve this.
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.
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