I have this working on my site, gathering the children and dumping them to unordered lists. (Off the top of my head) Try this:
// Recursive function - Since you'll be doing it with every resource,
// and Queries have limits, best to do it all in here.
function getMyChildren($parent, $atDepth, $toDepth)
{//Include MODX Object
global $modx;
$q = $modx->newQuery(
'modResource',
array('parent'=>$parent)
);
$q->sortby('menuindex', 'ASC');
$q->limit(999); // <-- DO set this appropriately (really, I mean it)
$q->prepare();
$children = $modx->getCollection('modResource', $q);
$output = array();
//CYA - Don't cycle, if children don't exist
if (!empty($children))
{//Cycle through all children
foreach ($children as $key => $child)
{//CYA - Don't output if it doesn't exist
if (!empty($child))
{
$output[] = $child->get('longtitle') . "\n";
// Recursively add Children
if ($toDepth >= $atDepth +1)
$output[] = implode("\n", getMyChildren($child->get('id'), $atDepth + 1, $toDepth));
}
}
}
return $output;
}
//CYA - Make sure starting ID exists
$start = $modx->getObject('modResource', $fromID); // <-- This is based on parameterized code.
// You can change to $modx->resource->get('id')
if (!empty($start)
{ $output = $start->get('longtitle') . "\n";
$output .= implode("\n", getMyChildren($fromID, 1, $toDepth));
}
return $output;
The above code can certainly be optimized, but recursive functions can be tricksy, so I figured I'd lay it out step by step.
[ed. note: fuzzicallogic last edited this post 14 years, 2 months ago.]