Get next post in the loop

Sometimes you need to access the next post in a loop, which is not created by Views, but when you are located on a Single Post.

For example, you want to display the Featured Image of the Post that comes next in the same Post Type, on the previous post.

The “next post” in the loop, can be accessed using the native WordPress function get_next_post()
https://codex.wordpress.org/Function_Reference/get_next_post

A ShortCode that returns the next post using this function can be crafted using these guidelines.
https://codex.wordpress.org/Shortcode_API

You should end up with:

function next_post_id( ) {
    $next_post_id = get_next_post()->ID;
    return $next_post_id;
}
add_shortcode( 'next-post-id', 'next_post_id' );

Now, you can use that in any ShortCode that supports an ID, in a single post, for example, to display the next’ posts’ image:

[wpv-post-featured-image item="[next_post_id]"]

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