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.


  current_post < 3) : ?>

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

About the author

Linjo Joson

Linjo is a PHP developer who loves to write about online businesses and marketing ideas

Add comment