This is a variation on other solutions.
Register the custom shortcode “loop-iteration” like so:
/** * Add loop-iteration shortcode * * Attributes * 'n' (optional) : test for nth iteration */ add_shortcode( 'loop-iteration', function( $atts ){ global $loop_n; if ( !isset( $loop_n ) ) { $loop_n = 0; return $loop_n; } $loop_n++; if ( !isset( $atts['n'] ) ) { // no nth parameter, just return the current index return $loop_n; } else { $remainder = ( $loop_n + 1 ) % $atts['n']; return ( $remainder == 0 ); } });
You can use this in either one of two ways inside the View loop output (but not both in the same loop, the shortcode must appear only once or the iterations will be wrongly counted).
Without ‘n’ parameter, the shortcode will simply output the current iteration.
With ‘n’ parameter it can be used with a conditional shortcode to only show some content for every nth iteration of the loop, like so:
[wpv-conditional if="( '[loop-iteration n='3']' eq '1' )"] <p>3rd item in loop</p> [/wpv-conditional]
Let us know if this snippet is not working for you:
This snippet doesn’t work