81 | Injecting Multiple adsense Ads into a WordPress Loop

g+

The versatile code injects for instance adsense ad codes several times into the posts of an index or archive page, starting with a pre-set post, and then repeated in regular intervals (up to a set limit of ads per page).

For example, in a child theme of Twenty Fourteen, this code would be added into content.php at a suitable location:

<?php 
//set the variables//
$nth = 3; //1,2,3,,4...; repeated every n-th post
$snr = 1; //1,2,3...; pre-set start with post nr
$limit = 3; //adsene TOS limit of ads per web page

$lsnr = $snr - 1; // correct for loop counter start at zero

if( $wp_query->current_post >= $lsnr && ( $wp_query->current_post - $lsnr )%$nth == 0 && (int)(( $wp_query->current_post - $lsnr ) / $nth ) < $limit ) { ?>
ADSENSE CODE
<?php } ?>

The example values would start the add in the first post, and repeated every three posts for a maximum of 3 ads.

This entry was posted in Tips & Tricks, Twenty Fourteen, WordPress and tagged , . Bookmark the permalink.

One Response to Injecting Multiple adsense Ads into a WordPress Loop

  1. Valencia comments:

    Excellent beat ! I would like to apprentice while you amend your site, how could i subscribe
    for a blog site? The account aided me a acceptable deal.
    I had been a little bit acquainted of this
    your broadcast offered bright clear idea

    Check out my web blog – Austin makeup artist
    (http://www.ileewei.cn/old/member.asp?action=view&memName=AudreaPurton44154565)

Leave a Reply

Your email address will not be published.

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=""> <s> <strike> <strong>

*