How to display adsense(advertisements) below first 3 (N) posts in wordpress only

Most or majority  of the wordpress bloggers use adsense (or advertisements) below posts. And in the home page (where we can display 3 ad units (adsense)  in total, each one below posts so as to display ads below the first 3 posts ). But the real issue is that the javascript code for the ads gets called again inside the wordpress loop and blank space will be shown there from the 4th posts onwards.

Normal Adsene below wordpress post title

Normal Adsene below wordpress post title

Adsense below the 4th post

Adsense below the 4th post

The $wp_query->current_post inside the wordpress will help you to fix this issues.  It will get the count of the current loop executed in the wordpress. And if the loop (post) is the 4th one , we can block the script from being run.

Remember that the wordpress loop count start with zero, you can limit the number of ad units displayed to 1 or 2 by changing the $wp_query->current_post < 3 as per you wish
 

Author: Linjo Joson

Myself Linjo Joson, here in dollarfry , I write articles about creating and optimizing websites as well as making money from your online business. I am PHP/WordPress developer by proffession.I love web analytics and driving traffic to them(doing this for 9 years). You can find me on Google+