Add Custom Advertisements Randomly in Blog Archives
This is an advanced javascript function you will have to modify it to work for your needs. Because of it's complexity it should be used only by people that know what they are doing and can modify it accordingly. Any questions regarding this snippet are completely outside the scope of support this is provided for your convenience only.
add_action( 'wp_footer', function() { ?>
( function( $ ) {
'use strict';
$( document ).on( 'ready', function() {
var $blog_entries = $( '#blog-entries' );
if ( $blog_entries.length ) {
// Your array of add codes
var $ads = [
'add 1 code',
'add 2 code',
'add 3 code'
];
// Add advertisement every 2nd item
// IMPORTANT: Make sure to change the classnames to match your blog's classnames
$blog_entries.children( ':eq(1)').after( '<article class="custom-ad blog-entry clr isotope-entry col span_1_of_2 grid-entry-style">'+ $ads[Math.floor(Math.random()*$ads.length)] +'</article>' );
// Add more adds on infinite scroll if it's enabled
// IMPORTANT: Requires Total 4.0+ to work correctly.
if ( $blog_entries.hasClass( 'infinite-scroll-wrap' ) ) {
$blog_entries.on( 'wpexinfiniteScrollLoaded', function( event, result ) {
result.children( ':eq(1)').after( '<article class="blog-entry clr isotope-entry col span_1_of_2 grid-entry-style">'+ $ads[Math.floor(Math.random()*$ads.length)] +'</article>' );
} );
}
}
} );
} ) ( jQuery );
<?php }, 1 );
All PHP snippets should be added via child theme's functions.php file or via a plugin (we recommend Code Snippets)