I can’t remember if I’ve released this yet or not, but it’s an extremely simple snippet for grabbing the content of random documents from a given folder. Would be easy enough to modify it to give another option of which field to return (title, longtitle, introtext, content, etc)
// snippet ShowRandom
// display content from random documents
// sottwell 01-2006
// arguments:
// &startDoc - ID of folder to select items from
// &numItems - number of random items to display
// &tpl = chunk name for item display template
$startDoc = isset($startDoc) ? $startDoc : $modx->documentIdentifier;
$numItems = isset($numItems) ? $numItems : 1;
$tpl = isset($tpl) ? $modx->getChunk($tpl) : '
<div class="randItem">
[+content+]
</div>
';
$resource = $modx->getActiveChildren($startDoc, 'createdon', 'DESC', $fields='id, pagetitle, description, introtext, content, createdon, createdby, hidemenu');
$limit = count($resource);
if(!function_exists(randArray)) {
function randArray($size, $min, $max) {
if ($size > $max)
$size = $max;
$v = array();
while ( count($v) < $size ) {
do {
$n = rand( $min, $max );
} while ( array_search($n, $v) !== false);
$v[] = $n;
}
return $v;
}
}
$list = randArray($numItems,0,$limit-1);
$items = '';
foreach($list as $item) {
$items .= str_replace('[+content+]',$resource[$item]['content'],$tpl);
}
return $items;