PHP Source Code


 * File randomChunk.php (requires MODx Revolution 2.1+)
 * Created on: 10/13/11 at 7:02 PM
 * Project shawn_wilkerson
 * @elements
 * @version 1.0
 * @category
 * @author W. Shawn Wilkerson
 * @link
 * @copyright Copyright (c) 2011, Sanity LLC. All rights reserved.
 * @license
$out = false;
$chunkPrefix = $modx->getOption('chunkPrefix', $scriptProperties, 'random');
$totalChunks = $modx->getOption('totalChunks', $scriptProperties, intval(0));

if ($totalChunks > 0) {
    $chunk = $modx->getObject('modChunk', array(
        'name' => $chunkPrefix . rand(1, ($totalChunks)),
if (is_object($chunk)) {
    $out = $chunk->process();
return $out;

Example Snippet Calls

[[!randomChunk?chunkPrefix=`banner`&totalChunks=`4`]] -- user selectable prefix
[[!randomChunk?&totalChunks=`29`]] -- defaults to random as the prefix

Example Output

Sample 2


Like most randomization code, this relies on a higher number of items to choose from to produce a more noticeable random effect. With fewer modChunks to choose from, it becomes no different than the predictability of flipping coins for heads and tails.

I also created a sub Category to minimize clutter in the Element Tree. This can also become the foundation of eventually implementing a random MODX Revolution Snippet centered on categories, where only the category name is required in the Snippet call.

Also notice, I force the direct process of the modChunk before its being sent to the page.