PHP Source Code

<?php

/**
 * File randomChunk.php (requires MODx Revolution 2.1+)
 * Created on: 10/13/11 at 7:02 PM
 * Updated on: 05/06/2015 at 14:43.
 * Project shawn_wilkerson
 * @version 2.0
 * @category
 * @author W. Shawn Wilkerson
 * @link http://www.shawnWilkerson.com
 * @copyright Copyright (c) 2011, Sanity LLC. All rights reserved.
 * @license GPL 2
 *
 * Version 2 Limited the scope of the incoming variables to allow the parser to better randomize multiple calls in the same page.
 */
$out = false;
$prefix = $modx->getOption('chunkPrefix', $scriptProperties, 'random');
$chunks = $modx->getOption('totalChunks', $scriptProperties, intval(0));

if ($chunks > 0) {
    $chunk = $modx->getObject('modChunk', array(
        'name' => $prefix . rand(1, ($chunks)),
    ));
}
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

Shawn is dependable, flexible, trustworthy, and professional. He takes pride in his work and his cost if very affordable. I highly recommend Mr. Wilkerson for any job...

Tony Barhoo, Pastor
Living Faith World Ministries

Shawn is dependable, flexible, trustworthy, and professional. He takes pride in his work and his cost if very affordable. I highly recommend Mr. Wilkerson for any job...

Tony Barhoo, Pastor
Living Faith World Ministries

Comments

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.

MODX 2.3+

MODX 2.1 - 2.2