Skip to content

Instantly share code, notes, and snippets.

@n7studios
Created January 21, 2015 16:44
Show Gist options
  • Save n7studios/15451be5989e3bcaf2a1 to your computer and use it in GitHub Desktop.
Save n7studios/15451be5989e3bcaf2a1 to your computer and use it in GitHub Desktop.
Soliloquy - Random Image Order
<?php
/**
* Plugin Name: Soliloquy - Random Image Order
* Plugin URI: http://soliloquywp.com
* Version: 1.0
* Author: Tim Carr
* Author URI: http://www.n7studios.co.uk
* Description: Randomizes the order of images in a slideshow. Optionally define a limit of how many slides to output.
*/
/**
* Randomize the order of images in a Soliloquy Slider
*
* @param array $data Slider Data
* @return string HTML
*/
function soliloquy_random_image_order( $data, $sliderID ) {
/**
* Change to list of slider IDs, comma seperated, that you want to apply randomization to.
* These can be obtained at WordPress Admin > Soliloquy > note the number in the "Shortcode" column.
*/
$slidersToRandomize = array( 376, 377, 378 );
/**
* Change to limit the number of slides to return. 0 or less denotes no limit
*/
$limit = 4;
// Don't edit anything below this line
// Check we are loading a slider that needs randomizing
if ( !in_array( $sliderID, $slidersToRandomize ) ) {
return $data;
}
// Shuffle keys
$keys = array_keys($data['slider']);
shuffle($keys);
// Rebuild array in new order
$new = array();
$count = 0;
foreach($keys as $key) {
$new[$key] = $data['slider'][$key];
$count++;
// Check if we need to enforce limit
if ( $limit > 0 && $count >= $limit ) {
break;
}
}
// Assign back to slider
$data['slider'] = $new;
// Return
return $data;
}
add_filter( 'soliloquy_pre_data', 'soliloquy_random_image_order', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment