$thisId = $modx->resource->get('id');
$ids = array_reverse($modx->getParentIds($thisId));
$ids[] = $thisId;
$i = 1;
$level = $modx->resource->getTVValue('levelsUp');
$output = '<p>In snippet id = ' . $thisId . '</p>';
foreach ($ids as $id => $value) {
$docIds[$i] = $value;
if ($value == $thisId) {
$target = $docIds[$i - $level];
if (!empty($target)) {
$url = $modx->makeUrl($target, "", "", "full");
if (!empty($url)) {
$output .= '<p>URL: ' . $url;
// $modx->sendRedirect($url);
} else {
$output .= '<p>URL is empty<br>Level is ' . $level . '<br> i is ' . $i . '</p>';
}
} else {
$output .= '<p>Target is empty<br>Level is ' . $level . '<br> i is ' . $i . '</p>';
}
}
$i++;
}
$output .= '<br><pre>' . print_r($ids, true) . '</pre>';
return $output;
return $thisId;
Code that works pasted below for reference - not it adds a ? before the #[[+id]] element - but still seems to work in a browser?
$thisId = $modx->resource->get('id');
$ids = array_reverse($modx->getParentIds($thisId));
$ids[] = $thisId;
$i = 1;
$level = $modx->resource->getTVValue('levelsUp');
$output = '<p>In snippet id = ' . $thisId . '</p>';
foreach ($ids as $id => $value) {
$docIds[$i] = $value;
if ($value == $thisId) {
$target = $docIds[$i - $level];
if (!empty($target)) {
$url = $modx->makeUrl($target, "", "#$thisId", "full");
if (!empty($url)) {
$output .= '<p>URL: ' . $url;
$modx->sendRedirect($url);
} else {
$output .= '<p>URL is empty<br>Level is ' . $level . '<br> i is ' . $i . '</p>';
}
} else {
$output .= '<p>Target is empty<br>Level is ' . $level . '<br> i is ' . $i . '</p>';
}
}
$i++;
}
$output .= '<br><pre>' . print_r($ids, true) . '</pre>';
return $output;
return $thisId;
$url = $modx->makeUrl($target, "", "", "full") . '#" . $thisId;
if (empty($level)) {
return '';
}
Try it this way:
$url = $modx->makeUrl($target, "", "", "full") . '#" . $thisId;
$url = $modx->makeUrl($target, "", "", "full") . '#' . $thisId;
BTW, if the TV is empty or contains a 0, you could end up reloading the current page indefinitely. This (as line 7) should prevent that:
if (empty($level)) { return ''; }