Insert Ads after first post on Wordpress front page - gHacks Tech News

Insert Ads after first post on WordPress front page

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.





  • We need your help

    Advertising revenue is falling fast across the Internet, and independently-run sites like Ghacks are hit hardest by it. The advertising model in its current form is coming to an end, and we have to find other ways to continue operating this site.

    We are committed to keeping our content free and independent, which means no paywalls, no sponsored posts, no annoying ad formats (video ads) or subscription fees.

    If you like our content, and would like to help, please consider making a contribution:

    Comments

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

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

    Leave a Reply