📜 ⬆️ ⬇️

Navigating nearby documents (Siblings)

Task: it is necessary to provide each child page with navigation “previous next”

All the few meager solutions that I found did not suit me: some did not work, some seemed very voluminous to solve such a trivial task. I decided to write my snippet, I hope that it will make life easier not only for me.


<?php
$ID = $modx->documentIdentifier;
$parentId = array_pop($modx->getParentIds($modx->documentIdentifier,1));

$children = $modx->getActiveChildren($parentId, 'menuindex' , 'ASC' );

$i=0;
$key = false ;
while (!$key && $i<count($children)){
$key = array_search($ID,$children[$i]);
$i++;
}

if (!empty($key) && count($children)>1){
$placeholders = array(
'prev' =>($i-2>=0? '<a href="' .$modx->makeUrl($children[$i-2][ 'id' ]). '">« </a>' : '' ),
'next' =>($i<count($children)? '<a href="' .$modx->makeUrl($children[$i][ 'id' ]). '"> »</a>' : '' )
);
$output = $modx->parseChunk( 'prevnext' ,$placeholders, '[+' , '+]' );

}
return $output;
?>


* This source code was highlighted with Source Code Highlighter .

')
Snippet displays the result in the prevnext chunk, which uses two placeholders:
[+ prev +]
[+ next +]

It even works;)

Source: https://habr.com/ru/post/71722/


All Articles