Sometimes, you want to count the number of iterations a View has gone through.

You can use Toolset to add the following code to your site. It will add a custom shortcode.

Code Snippet
<?php
toolset_snippet_security_check() or die( 'Direct access is not allowed' );
/**
 * Snippet for working with a View loop counter.
 *
 * Automatically records the View loop counter before rendering a loop item.
 *
 * Provides a shortcode for rendering the current View loop.
 * Provides a shortcode for forcing a new loop counter increase for pad and pad-last indexes.
 * Provides a shortcode for clearing the View loop counter if the same View will be added to the same page more than once.
 */
class Tssnippet_Loop_Iteration {
	private $loop_counter_per_view = array();
	
	public function __construct() {
		// Automatically record the curent View loop before rendering one loop item
		add_action( 'wpv_action_wpv_loop_before_display_item', array( $this, 'record_loop_for_view' ), 10, 3 );
		
		// Define shortcodes for accessing, forcing or clearing the View loop count
		// No shortcode has attributes
		add_shortcode( 'tssnippet-loop-iteration', array( $this, 'get_loop_iteration' ) );
		add_shortcode( 'tssnippet-loop-iteration-force', array( $this, 'force_loop_iteration' ) );
		add_shortcode( 'tssnippet-loop-iteration-clear', array( $this, 'clear_loop_iteration' ) );
	}
	
	/**
	 * Get the loop counter for a given View.
	 *
	 * @param int $view_id
	 * @return int The count of the View loop, starting at 1, or 0 if outside a View loop.
	 * @access private
	 */
	private function get_loop_counter_per_view( $view_id ) {
		if ( ! array_key_exists( $view_id, $this->loop_counter_per_view ) ) {
			$this->loop_counter_per_view[ $view_id ] = 0;
		}
		
		return $this->loop_counter_per_view[ $view_id ];
	}
	
	/**
	 * Set the loop counter for a given View.
	 *
	 * @param int $view_id
	 * @param int $counter
	 * @access private
	 */
	private function set_loop_counter_per_view( $view_id, $counter ) {
		$this->loop_counter_per_view[ $view_id ] = $counter;
	}
	
	/**
	 * Automatically update the View loop counter right before rendering a View loop item.
	 *
	 * @param mixed $item The object about to be rendered, changes depending on the View type.
	 * @param string $query_type The View query type.
	 * @param int $view_id
	 */
	public function record_loop_for_view( $item, $query_type, $view_id ) {
		$current_count = $this->get_loop_counter_per_view( $view_id );
		$this->set_loop_counter_per_view( $view_id, $current_count + 1 );
	}
	
	/**
	 * Render the current View loop index.
	 *
	 * @return int The current View loop index, or zero if outside of a View loop.
	 */
	public function get_loop_iteration() {
		$current_view = apply_filters( 'wpv_filter_wpv_get_current_view', 0 );
		if ( $current_view ) {
			return $this->get_loop_counter_per_view( $current_view );
		}
		
		return 0;
	}
	
	/**
	 * Force an extra loop counter increase for the current View loop.
	 * Useful to use in pad and pad-last loop indexes, which do not increase the loop counter automatically.
	 */
	public function force_loop_iteration() {
		$current_view = apply_filters( 'wpv_filter_wpv_get_current_view', 0 );
		if ( $current_view ) {
			$this->set_loop_counter_per_view( $current_view, $this->get_loop_counter_per_view( $current_view ) + 1 );
		}
	}
	
	/**
	 * Clear the current View loop counter.
	 * Useful to include after the <!-- wpv-loop-end --> comment in case you want to add the same View multiple times to the same page.
	 */	 
	public function clear_loop_iteration() {
		$current_view = apply_filters( 'wpv_filter_wpv_get_current_view', 0 );
		if ( $current_view ) {
			$this->set_loop_counter_per_view( $current_view, 0 );
		}
		
		return;
	}

}

new Tssnippet_Loop_Iteration();

This snippet will automatically record the counter of iterations for all Views. It provides three shortcodes to interact with those counters:

  • tssnippet-loop-iteration returns the iteration counter for the current View
  • tssnippet-loop-iteration-forcewill force an increase in the counter for the current View loop. It can be used to deal with thepadandpad-last indexes of the View loop, which are not recorded automatically
  • tssnippet-loop-iteration-clearwill clear the counter for the current View. It should be used after the View loop is rendered, to clear the counter, if you plan to use the same View more than once in the same page.

These shortcodes have no attributes.

If you want to check the loop counter and display content based on it, please register the tssnippet-loop-iterationshortcode as a condition source as detailed in our documentation. After doing this, you will be able to use it like this:

Display 3rd item in loop
[wpv-conditional if="( '[ltssnippet-loop-iteration]' eq '3' )"]
3rd item in loop
[/wpv-conditional]

You can use Toolset to easily add custom PHP code to your site without editing any theme files.