Displaying Ads After Selected Number of Posts In WordPress

April 27th, 2009

At wphacks.com, I found a solution for displaying ads after the first post only. I had thought about using it, as it would’ve worked fine for the project I needed it on. But, I decided to use a counter instead as I thought it would be more flexible. For instance, what if you want the ad to appear after the second post… or after the third post? The following code worked good for me, and I hope it does the same for you:

/*Code to initiate counter goes above the loop*/
<?php
    /*Initiate counter for post ads*/
    $counterAd = 1;
?>
 
/*Start of loop followed by post content here*/
 
/*Before the end of the loop, insert the ad code and counter incrementation:*/
<?php if($counterAd == 1){
 /*If $counterAd = 1 that means this is just after the first post.  If so, print ad.*/
/*NOTE: Alternatively you could change the one to be whichever post you'd like the ad to display after.*/
?>
	 <!-- Ad Code Goes Here -->
<?php
      }
?>
<?php
/*Increment counter*/
$counterAd ++;
?>

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • E-mail this story to a friend!
  • Technorati

1 Comment »

  1. Oh my goodness! Amazing article dude! Thank you,
    However I am encountering difficulties with your RSS.
    I don’t know why I cannot subscribe to it. Is there anyone else
    getting the same RSS problems? Anybody who knows the answer will you kindly respond?
    Thanx!!

    Comment by relationship tips — 25 February 2015 @ 4:40 pm

RSS feed for comments on this post. TrackBack URL

Leave a comment