×

How to insert Ads after 1st Post in WordPress

Google Ads after 1st Post

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 )

[code]
<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>
[/code]

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

[code]
<?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 }?>   
[/code]

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

rakesh

The Whole concept was developed by the man you are watching here. I am rakesh Kumar Serial Niche Blogger and SEO Enthusiast. find me on Google+ , facebookandon twitter.. Developed popular WordPress theme for Wallpapers and funny Image WordPress theme. Love SEO optimized WordPress theme Designing and Customization.
Related Posts

3 thoughts on “How to insert Ads after 1st Post in WordPress”

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

  2. 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

    1. 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. :)

Leave a Reply

Your email address will not be published.

+-