• Home
  • About John P.
  • Contact
  • Terms
  • Facebook
  • LinkedIn
  • Pinterest
  • Twitter
  • YouTube

One Man's Blog

Specialization is for Insects.

  • 10 Best
  • Art
  • Computing
  • Fun Stuff
  • Gadgets
  • Videos
  • Photography
  • Travel
  • Tutorials
  • Health & Fitness
  • Politics
  • Thoughts
  • Food
  • Reviews
  • 10 Best
  • Art
  • Computing
  • Fun Stuff
  • Gadgets
  • Videos
  • Photography
  • Travel
  • Tutorials
  • Health & Fitness
  • Politics
  • Thoughts
  • Food
  • Reviews
Home / Tutorials / WordPress: Homepage Ads ONLY After The First Article

WordPress: Homepage Ads ONLY After The First Article

John P.

October 3, 2007 By John P.

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!

  1. In the PRESENTATION menu, under THEMES EDITOR, find the theme file called “Main Index Template” (index.php).
  2. 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.
  3. Find a line of code that looks much like the following: <?php the_content('Read the rest of this entry »'); ?>
  4. Immediately after that, paste the following: <?php if ($count == 0) : ?> ENTER YOUR AD CODE HERE <?php endif; $count++; ?>
  5. 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.

Related

Filed Under: Tutorials, Wordpress Tagged With: Advertising, Themes, Tutorials, Wordpress

About John P.

John P. is a former CEO, former TV Show Host, and the Founder and Wizard behind Texas Metal Works. You can find him on Twitter, Facebook and LinkedIn. Feel free to send shoutouts, insults, and praise. Or Money. Money is good.

Comments

  1. Mihir Naik says

    November 4, 2011 at 10:46 pm

    Hey The problem is, I am seeing same ads on individual post also. What to do?

  2. Ali Hussain says

    September 23, 2009 at 3:22 am

    Thankyou but you can use other ways too.
    See here.
    http://www.bitrepository.com/insert-google-adsense-ads-between-your-wordpress-blog-posts.html

  3. Durkin - Go Holga says

    October 23, 2008 at 1:29 pm

    worked great, thanks!

  4. Todor Christov says

    September 13, 2008 at 8:15 am

    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?

  5. Shawn says

    July 31, 2008 at 2:40 pm

    That’s what I figured but I tried that and it broke the page.

  6. John P. says

    July 30, 2008 at 6:20 pm

    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

  7. Shawn says

    July 30, 2008 at 12:32 pm

    What would the code look like if we wanted an ad after the first *and* second posts?

  8. Eduardo Maio says

    February 25, 2008 at 6:44 pm

    Such a small piece of code for a nice big result! Thank you!

  9. Anak Medan says

    January 24, 2008 at 5:16 am

    I have try your code and its worked !
    thanx for nice post Dear :)

  10. melayu says

    October 5, 2007 at 11:51 pm

    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

  11. John P. says

    October 5, 2007 at 10:32 pm

    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

  12. melayu says

    October 5, 2007 at 10:02 pm

    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”

  13. TheDane says

    October 5, 2007 at 6:58 am

    I just rewrote some of the code in the Adsense Injection plugin… I like that plugin a lot, makes my life easier…

    Kim:)

  14. Donncha O Caoimh says

    October 5, 2007 at 3:44 am

    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++;

  15. John P. says

    October 5, 2007 at 3:38 am

    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

  16. Urbanist says

    October 3, 2007 at 6:37 pm

    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.

  17. Dax says

    October 3, 2007 at 3:25 pm

    That is pretty straight forward. I thought there was much more code required to achieve this.

  18. Donncha O Caoimh says

    October 3, 2007 at 1:37 pm

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

  19. John P. says

    October 3, 2007 at 12:43 pm

    Yes, it helps a little… Here’s a question:

    if( is_regular_user() == false ) {
    // adsense code
    }

    if( is_old_post() ) {
    // adsense code
    }

    if( is_searchengine_user() ) {
    // adsense code
    }

    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

  20. Donncha O Caoimh says

    October 3, 2007 at 11:56 am

    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?

  21. MG says

    October 3, 2007 at 11:55 am

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

  22. John P. says

    October 3, 2007 at 11:48 am

    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

  23. Donncha O Caoimh says

    October 3, 2007 at 7:30 am

    Go on John, give my friends adverts plugin a plug! :)

  24. Tom Barr says

    October 3, 2007 at 1:54 am

    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.

John P’s Tweets

Tweets by johnpoz

Read Over 2,500,000 Times

  • How I'd Hack Your Weak Passwords
  • Amusing Little Helicopter Game
  • A Compendium of 150 Monty Python Sketches
  • 10 Worst Drivers Ever Caught On Video
  • Get Your Virtual Haircut and Other Auditory Illusions
  • How Crack Cocaine is Made

Copyright © 2023 · Local Media on Genesis Framework · WordPress · Log in