Tutorial: How To Put Ads on WordPress Home Page Only

Ads on WordPress HomepageFolks, since my site has gotten fairly popular I have people who want to advertise on the home page. The problem is that I didn’t know of a good method of embedding Ads that only show up on the home page and don’t appear site wide. (And not in the sidebar.)

I needed to employ a little PHP trickery in order to get this to work and I figured I’d share the methodology in case anyone else wanted to do this, or had a better idea as to how to pull it off.

In my case what I did was edit the Index.php file and added the following code to the bottom of the page inside the loop:
EDIT: Thanks to Storm for dropping me a comment with an even simpler method which came from Douglas Karr. Here it is:

<?php if(is_home() && $post==$posts[0] && !is_paged()) { ?>
     PLACE ADVERTISEMENT HERE.
<?php } ?>

What this does is causes the ads to appear on the Homepage, but not on any of the other Category or Archive “pages”.

Comments

  1. Storm says:

    Of course if someone knows how to get them to ONLY appear on the root homepage I’d appreciate the tip.

    I was looking for just that, and found it here:

    http://www.douglaskarr.com/2006/09/06/wordpress-hack-after-the-first-post-only-on-the-home-page/

  2. John P. says:

    Storm,

    Thanks for the tip it worked perfectly! I’ve updated the article to reflect the change. I really appreciate it.

    John

  3. johnnykarout says:

    Thank you a lot for this hint, I will start using it after I add this comment :)

  4. Vishal says:

    Nice one. Will definitely plug it in my blog. Thx :-)

  5. dave says:

    Thanks for the code–I will certainly implement it on my blogs.

    Does anybody know of similar code that can be used for any/all php scripts, not just wordpress?

  6. Madel says:

    Hi, I am wondering how can I go to the index.php to my theme, I realli want to put ads on my blog but can’t do it yet..help pls..

  7. Brandon says:

    To bad I don’t use adsense on most of my blogs :))

  8. heris says:

    Many many thanks for the tip. I have been looking for the answer on how to do it with Tigopedia Reloaded WP theme (it doesn’t separate page for homepage and category/archive), and your tip works very well.

  9. John P. says:

    Glad that helped you out Heris! :-)

  10. John P. says:

    Sorry Madel, it looks like you are using WordPress.com and I’m not sure how, or if, you can edit the themes there. This tip was really for the stand-alone WordPress that can be downloaded from WordPress.org.

    John

Speak Your Mind

*