Instant download after payment Secure checkout Contact support

WordPress How to Tutorials

Add Ad Code After Every Few Post

23 July 2026

How to Insert or add ad code after every few post in WordPress post loop. Many of users or asking me this question because if you have large number of post in your blog and you want to include add code for Google Adsense code after you post so you can boost your earning. Like I always say we learn from our our experience same thing I want to do with my blog so I did Research and comes with a code that helps that helps me and hope everyone can use it First you should know that you must have a little bit PHP knowledge or at least understand that text writing and closing because if you made any changes in core file and that goes wrong the new site might be goes offline or show error that you need to fix from server side be sure take full site backup first or at least theme files. Also if you are using Google Adsense code then you must familiar with Google Adsense policy as well because using too much ad and low content then your site may harm else you are using any other banner of promo code.
<?php /* Start the Loop */ ?>
 <?php while ( have_posts() ) : the_post(); ?>

 <?php

 get_template_part( 'template-parts/content', get_post_format() );
 ?>
<?php $postnum++; if($postnum%8 == 0) { ?>
 YOUR AD CODE
 <?php } ?>

 <?php endwhile; ?>

 <?php the_posts_navigation(); ?>

 <?php else : ?>

 <?php get_template_part( 'template-parts/content', 'none' ); ?>
 <?php endif; ?>
This is main code paste into a post loop and change %8 to any number you want for your ad code to show.
<?php $postnum++; if($postnum%8 == 0) { ?>
 YOUR AD CODE
 <?php } ?>
If you have any question regarding this topic let me know in comment section below. As a consideration use this code in archive pages as well to show your advertisements and Banner in category pages and navigation pages again I strongly recommend you to take backup to be sure.

5 comments

  • Dr. Md. Makhluk Hasan 1 November 2016
    Helpful articles for all users.
  • Anand Kumar 11 August 2017
    helpful article for me
  • Alal 30 April 2018
    How can i insert 2 or 3 position?
  • nik 1 December 2018
    which file i can put this code ?
  • G PRASAD 6 December 2018
    Good Article &amp; Thankyou