I wrote a function to help with this to functionally mimic Ruby on Rails’ cycle
helper:
/**
* Cycle/alternate unlimited values of a given array.
*
* For instance, if you call this function five times with
* `cycle_it( 'three', 'two', 'one' )`, you will in return get:
* three two one three two
*
* This is useful for loops and allows you to cycle classes.
*
* Example:
*
* foreach ( $posts as $post ) {
* printf(
* '<div class="%s">...</div>',
* esc_attr( cycle_it( 'odd', 'even' ) )
* );
* }
*
* This would alternate between `<div class="odd">...</div>` and
* `<div class="even">...</div>`.
*
* You can pass any data as args and as many as you want, e.g.
*
* cycle_it( array( 'foo', 'bar' ), false, 5, 'silly' )
*
* @param mixed ...$args Any number of arguments to cycle.
* @return mixed Alternating value passed through the function arguments.
*/
function cycle_it() {
static $cycle_curr_index;
$args = func_get_args();
$fingerprint = sha1( serialize( $args ) );
if ( ! is_array( $cycle_curr_index ) ) {
$cycle_curr_index = array();
}
if ( ! is_int( $cycle_curr_index[ $fingerprint ] ) ) {
$cycle_curr_index[ $fingerprint ] = -1;
}
$cycle_curr_index[ $fingerprint ] = ++$cycle_curr_index[ $fingerprint ] % count( $args );
return $args[ $cycle_curr_index[ $fingerprint ] ];
}
If you add it to your theme’s functions.php file, you can then change your code to this:
echo <div id="seasonBlock" class="span-12' . esc_attr( cycle_it( '',' last' ) ) . '" >';