Displaying Ads After a Certain Number of Posts

Ads are an integral part of some websites, and with Themify themes, not only you can show them using widgets or editing templates, but also making use of the action hooks defined in themes.

1. create a file named custom-functions.php in the theme root directory and paste the following

<?php
/* Define post counters and flag to know when the ad is shown  */
global $themify_count_posts, $themify_index_ad_shown;
$themify_count_posts = 1;
$themify_index_ad_shown = false;
/**
 * Add an add in single post view and in 3rd post in index view
 */
function custom_themify_ad() {
    global $themify_count_posts, $themify_index_ad_shown;
    if( is_archive() || is_home() ) {
        if ( 0 == $themify_count_posts % 3 && !$themify_index_ad_shown ) {
            $themify_index_ad_shown = true;
        ?>
            <div class="themify-custom-ad themify-index-ad">
                <p>THIS IS YOUR INDEX AD</p>
            </div>
        <?php }
        $themify_count_posts++;
    } elseif( is_single() ) {
        ?>
            <div class="themify-custom-ad themify-single-ad">
                <p>THIS IS YOUR SINGLE AD</p>
            </div>
        <?php
    }
}
add_action( 'themify_post_end', 'custom_themify_ad');
?>

2. save the file. If you were editing it locally, upload it by FTP to the theme folder in your server.

Themes with Infinite Scroll

In themes with infinite scroll (eg. Pinboard, Grido, Metro), it will still work, but for the ad placement to be reliable, the number of posts loaded each time must be divisible by the number of posts that will be skipped before adding the ad. Otherwise the ad position will be offset each time a new set of posts is loaded.

For example:
if you're loading 10 posts per page, you can add an ad after 1, 2 or 5 posts
if you're loading 8 posts per page, you can add an ad after 1, 2 or 4 posts

The following sample code works for both infinite and regular themes and displays an text cyclically after 4 posts

<?php
/* Define post counter */
global $themify_count_posts;
$themify_count_posts = 1;
/**
 * Add an add in index view after 4 posts
 */
function custom_themify_ad() {
    global $themify_count_posts;
    if( is_archive() || is_home() ) {
        // The number 4 below is one that defines how many posts
        // will be skipped before adding the ad.
        if ( 0 == $themify_count_posts % 4 ) {
        ?>
            <div class="themify-custom-ad themify-index-ad">
                <p>THIS IS YOUR INDEX AD</p>
            </div>
        <?php }
        $themify_count_posts++;
    }
}
add_action( 'themify_post_end', 'custom_themify_ad');
?>

To give an example with the code above would be the opposite, since an ad will be added every 3 posts, the posts loaded each time should be 6, 9, 12, 15 and so on.

Fix Google Ads in InfiniteScroll

Then add the following code in Themify > Settings > General => Footer Code to fix the Google Ads scripts removed by InfiniteScroll script:

<script>
jQuery(document).on('newElements', function(){
    (adsbygoogle = window.adsbygoogle || []).push({});
});
</script>