Add Ad Code After Every Few Post


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(); ?>


 get_template_part( 'template-parts/content', get_post_format() );
<?php $postnum++; if($postnum%8 == 0) { ?>
 <?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) { ?>
 <?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 responses to “Add Ad Code After Every Few Post”

  1. Dr. Md. Makhluk Hasan Avatar

    Helpful articles for all users.

  2. Anand Kumar Avatar

    helpful article for me

  3. Alal Avatar

    How can i insert 2 or 3 position?

  4. nik Avatar

    which file i can put this code ?

  5. G PRASAD Avatar

    Good Article & Thankyou

Leave a Reply

Your email address will not be published. Required fields are marked *