How to insert Ads after 1st Post in WordPress


Today, We are going to insert Google Adsense code after 2 post. This code can be implemented on index.php file, category.php, author.php or any other WordPress theme file.

In below code, I am using Bootstrap to render my theme thus most CSS styling tags are coming from bootstrap CSS.( Assuming that you have working knowledge of bootstrap )

<div class="col-md-8 col-lg-8 col-sm-6 col-xs-12">     
    <?php $i=0; while(have_posts()) : the_post();  ?>
         <?php if($i==2 || $i==8) { ?>
              <p class="bg-primary"><?php echo $food['below-title'];?></p>
          <?php }?>   
        <div class="panel panel-default no-radius box-shadow">
        <?php the_post_thumbnail('medium'); ?>  
        <div class="panel-body">
            <div class="quote">
                <a href="<?php the_permalink();?>"><?php the_title();?></a>
             <?php the_excerpt();?>
            </div>
            <div class="pull-left author"><?php the_author_posts_link(); ?></div>
        </div>
        <div class="panel-footer">
                 <span style="text-transform:capitalize;"><?php the_tags(' ', ' , ' ); ?></span><span class ="pull-right" style="padding:0px 10px;"><i class="fa fa-facebook-square"></i></span><span class ="pull-right" style="padding:0px 10px;"><i class="fa fa-twitter-square"></i></span><span class ="pull-right" style="padding:0px 10px;"><i class="fa fa-google-plus-square"></i></span>
        </div>
        </div>
      <?php $i++; endwhile; ?>
</div>

In the above code, we have introduced a new variable $i and after each post, it increment itself by 1. Since we need to display our
Google adsense ads after 1st post and 8th post this added this code here

<?php
         $google_ads ="Google Adsense code goes here";
        
         $i=0; while(have_posts()) : the_post();  ?>
         <?php if($i==2 || $i==8) { ?>
              <p class="bg-primary"><?php echo $google_ads ;?></p>
          <?php }?>   

Rest will remain same. Just save your file and refresh to check its output. My output is something like this. ( Do not Worry This screenshot is taken from my forth coming WordPress theme for Quotation.Google Ads after 1st Post




3 Brilliant Comments - Join Discussion Now!

  1. Hello Rakesh
    I think its diffult for me and others WordPress user who don’t know coding
    Are you know any plugins who can help us for this process or adding ads …
    Have nice day

    • rakesh says:

      Agreed Sachin, It is little bit tricky for non-programmers thus provided Samriddhi my free adsense ready WordPress theme, If you want this functionality in any of your theme, Just mail me your theme and I would love to do this for you. :)

  2. Hi Rakesh
    Now I have use free Point WordPress theme on site …. So you can something like adding ads for posts one By one

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>