Display custom content after any “n” number of items in a View Loop

Imagine if you would like to add an Ad code (or code snippet) in loop sequence after every 2 runs (number of items) through the loop, whereas each Ad code is unique. Then you can use the following code and shortcode [ad_incrementor n_number=”2″ ] to achieve this.

Code for your theme’s or child theme’s functions.php file:

add_shortcode('ad_incrementor', 'ad_incrementor');
function ad_incrementor($atts, $content = null) {

extract( shortcode_atts( array(
'n_number' => '3',
), $atts ) );

$ad_array = explode(',' , $content);

static $i = 1;
static $j = 0;
$n = $i ++;
if( $n % $n_number == 0 ) {
$m = $j;
$j++;

return $ad_array[$m];
}
}

Example shortcode usage for the View:

[ad_incrementor n_number="2" ]
Your custom content or ad code snippet goes here,
Next custom content goes here,
[/ad_incrementor]

More details and example usage here:
https://wp-types.com/forums/topic/adding-ad-code-for-every-n-in-a-loop/#post-539691