Insert Ads after first post on WordPress front page

Martin Brinkmann
Oct 15, 2012
Development
|
6

Advertisement can be placed in several locations on a WordPress blog. Back in 2005 I used plugins to display ads here and there for a while until I figured out how to do it manually instead. The advantage of that is that you do not run another plugin on the site so that you free up resources for relevant things. As far as I remember, there were also plugins that automatically inserted the authors Adsense ID into a percentage of all ad displays on the website which probably made me find out about manually adding ads into WordPress in first place.

I recently had to insert ad code for select campaigns on the WordPress front page so that ads appear after the first post on the index if a campaign is running. Most WordPress webmasters who try to do that for the first time usually make mistakes which may result in ads being displayed after each post on the front page, or after all posts there.

It is however relatively easy if you know how to do it. Below is a screenshot of the source code that I use here on Ghacks. Yellow highlights the code that you need to add to the blog's index.php file, red the line that you need to add it before.

wordpress index display ads

Code to insert ads on WordPress' front page (the index.php file):

<?php if(!$show_ads){ ?>
ad code or content
<?php $show_ads = 1; } ?>

You need to insert the code before:

<?php endwhile; ?>

The show_ads variable defines the post after which the content is displayed. If you want to display the ads after the second or third post, you can do so by changing the 1 to 2 or 3 instead.
I suggest you test it with a simple line of text to make sure it is displayed in the correct location on the WordPress front page. Once that has been verified, exchange the text to the ad or content code.

This should be compatible with all recent versions of WordPress. Please note that some WordPress themes may require you to edit a different file to add the code to the front page of the blog. I suggest you try it out on a local blog first before you push the updated code on the server.

Advertisement

Previous Post: «
Next Post: «

Comments

  1. James said on September 21, 2015 at 6:42 pm
    Reply

    Thanks for the tutorial. It worked perfectly but I need to add the ads after 3 posts in the homepage. It dint work for me even after changing the count number to three.

  2. RG said on October 15, 2012 at 4:00 pm
    Reply

    I would remind everybody to use a child theme. But I suspect if working with an actively developed theme and depending on how it is written index.php might change enough times that you may need to edit index.php in the child theme folder sometimes too.

Leave a Reply

Check the box to consent to your data being stored in line with the guidelines set out in our privacy policy

We love comments and welcome thoughtful and civilized discussion. Rudeness and personal attacks will not be tolerated. Please stay on-topic.
Please note that your comment may not appear immediately after you post it.