Tutorial: How To Put Ads on WordPress Home Page Only

by John P.

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


{ 34 comments… read them below or add one }

1 Storm July 12, 2007 at 5:19 pm

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/

Reply

2 John P. July 12, 2007 at 10:05 pm

Storm,

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

John

Reply

3 johnnykarout July 16, 2007 at 8:12 am

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

Reply

4 Vishal July 19, 2007 at 10:09 am

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

Reply

5 dave August 22, 2007 at 10:23 am

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?

Reply

6 Madel September 13, 2007 at 2:46 pm

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

Reply

7 Brandon September 13, 2007 at 5:25 pm

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

Reply

8 heris September 14, 2007 at 12:15 pm

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.

Reply

9 John P. September 14, 2007 at 3:54 pm

Glad that helped you out Heris! :-)

Reply

10 John P. September 14, 2007 at 3:56 pm

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

Reply

11 nefesco November 21, 2007 at 11:37 am

WOW! Easy and effective! Thanks lot man ^_^

P/S: Why do u have TWO *subscribe-to-comment* (top and below)? Izzit redundant? No offend, just askin if u might not have seen it… thx again for the wonderful tip :)

Reply

12 femikey February 25, 2008 at 11:23 am

hi i want to ask something about how to put ads at wordpress homepage only? because i have a wordpress blog but when i check the presentation i dont have index.php i only have themes, widgets, extras, custom image header and css stylesheet editor. How could i add the url that only shows to my homepage if i use the css stylesheet editor? I want to know this because there’s a company offer me to add their link on my link that shows only to my wordpress homepage and not with the other links for my post. Can you help me please? thanks..

femikey

Reply

13 John P. February 25, 2008 at 5:58 pm

Femikey,

When you are in the admin control panel and select “Presentation”, you should see a tab that says “Theme Editor”. It is when you are within the theme editor that you can make the changes that I previously listed.

In the theme editor find the index.php page and add the code whereever you want the ads to appear. You may have to experiment a bit to get it in just the spot you want it, so create a backup of the original code before you get to messing with things just to ensure you can revert back if necessary.

John

Reply

14 John P. March 14, 2008 at 7:05 pm

Gio,

Can you give me a little more specific information on exactly what happened and perhaps I can help you troubleshoot it?

John

Reply

15 Ronny Domain May 3, 2008 at 9:57 pm

When I need it for my new template, I found your post by googling with keyword ‘ only on homepage “wordpress” ‘
That’s cool. It’s working at all. Thank you.

Reply

16 Shops May 14, 2008 at 6:40 am

I have a similar problem – Anyone know how to show the page number on the index page when you go prev next pages?

Reply

17 Johnny May 21, 2008 at 5:29 pm

Happy that I found this piece of code

Reply

18 andrew August 18, 2008 at 7:46 pm

Thanks for the code.
Happy I can find your post on it.
Really helpful.

Reply

19 ika November 4, 2008 at 9:47 pm

thanks very very much!!! i googled about this by typing ‘ads on homepage only’ and your blog’s at the top! so thanks for posting about this code..

Reply

20 Michael Murdock November 5, 2008 at 4:25 pm

I loved this piece of code as I was able to drop it into a footer which is where I wanted the string of ads to show. This is great for companies that do a php string where they’re pulling from their server and dropping text into your page every time someone signs up for an ad. Wooohooo! Now ads only on the front page. Nice coding. Thanks for making it available.

Reply

21 Rob C December 10, 2008 at 5:24 pm

Ha! – the perfect solution to an issue I had with one of my sites! If only I’d thought to Google it earlier, instead of wasting my time *not* working out how to do it…

Definitely linking you from my blog on the next update

Thanks a lot!

Reply

22 Dark orkut January 21, 2009 at 7:23 am

Thanks mate. It will help me to monetize my site well.

Reply

23 Rana January 29, 2009 at 9:51 pm

It works like a charm, thanks for your tutorial.

Reply

24 Fred June 19, 2009 at 5:35 pm

Thanks for the help with the code. I have however some trouble with my requested advert as it needs to be incontent.

for example: A
anchor
bit of blurb here

The bit of blurb at the end is coming up fine, but I can’t seem to workout how to get the ‘A’ to appear before the link..

Any help would be great..

I have bookmarked your site, very helpful indeed, thanks

Reply

25 Bill July 4, 2009 at 8:32 pm

Thanks for posting the elegant code that allows you to only display particular content on the wordpress homepage. I was racking my brain all afternoon trying to figure out a way to do this, then I stumbled upon your site and it made my night! Thanks!

Reply

Leave a Comment

Previous post:

Next post: