getAncestors - get a list of all parents resource ids

I wanted something like google tag manager to insert code on a url match. Basically any page on a certain path.

example.com/articles/hardware/mods

where “hardware” is resource 85

[[getAncestors? &id=85 &tpl=message]]

// getAncestors
$id = $modx->getOption('id', $scriptProperties);
$tpl = $modx->getOption('tpl', $scriptProperties, 'message');
$depth = $modx->getOption('depth', $scriptProperties, 10);
$output = '';
$myid = $modx->resource->get('id');   
$parents = $modx->getParentIds($myid, $depth);   
$modx->setPlaceholder('ancestors', implode(",", $parents));
if (in_array($id, $parents)) {
   $output = $modx->getChunk($tpl);
} 
return $output;
1 Like