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

 <?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 thoughts on “Add Ad Code After Every Few Post

Leave a Reply

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

Name *
Email *
Website