Displaying Ads After Selected Number of Posts In WordPress
April 27th, 2009At 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 ++; ?>










