Skip to content

Instantly share code, notes, and snippets.

@dubrod
Last active November 30, 2018 00:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dubrod/723bd868d84f45a937276804c16d1692 to your computer and use it in GitHub Desktop.
Save dubrod/723bd868d84f45a937276804c16d1692 to your computer and use it in GitHub Desktop.
Simpler MODX Sibling Nav for all children with just previous and next output
[[!siblingNav? &parents=`4` &limit=`4` &sortBy=`{"publishedon":"DESC"}` &prevTpl=`mxt.prevTpl` &nextTpl=`mxt.nextTpl`]]
<ul class="clearfix">[[+sn.prev]][[-+sn.prevlinks]][[-+sn.nextlinks]][[+sn.next]]</ul>
PREV
<li>[[+_isactive:is=`1`:then=`<a href="[[~[[+id]]]]">&laquo; [[+pagetitle]]</a>`:else=` &laquo; `]]</li>
//////
/*
* Wayne Roddy (wayne@modx.com)
* Dec 1 2016
* Snippet for retrieving children with a {Specific} Template
* Creating an Array and finding the siblings for Placeholders
* Used on the News Item Page
*
* [[GetSiblingstoPH? &tpl=`15`]]
*/
$siblings = [];
$activeId = $modx->resource->get('id');
$parentId = $modx->resource->get('parent');
$tpl = $modx->getOption('tpl', $scriptProperties);
$q = $modx->newQuery('modResource');
$q->where(array('published'=>'1','parent'=>$parentId,'template'=>$tpl,'class_key'=>'modDocument'));
$q->sortby('publishedon','DESC');
$resources = $modx->getCollection('modResource',$q);
foreach($resources as $r) {
$id = $r->get('id');
$title = $r->get('pagetitle');
$s = array("id" => $id,"title" => $title);
array_push($siblings, $s);
}
//print_r($siblings);
function activeKey($data, $field, $value){
foreach($data as $key => $item){
if ( $item[$field] === $value )
return $key;
}
return false;
}
$active_key = activeKey($siblings,'id',$activeId);
//make previous html
$prevkey = $active_key - 1;
//$modx->log(modX::LOG_LEVEL_ERROR,'previous key: ' . $prevkey);
if($prevkey >= 0){
$prevurl = $modx->makeUrl($siblings[$prevkey]["id"]);
$prevChunk = $modx->getChunk("sibling-prev", array("url"=>$prevurl,"title"=>$siblings[$prevkey]["title"]));
} else {
$prevChunk = $modx->getChunk("sibling-empty", array());
}
//make next html
$nextkey = $active_key + 1;
//$modx->log(modX::LOG_LEVEL_ERROR,'next key: ' . $nextkey);
if($nextkey < count($siblings)){
$nexturl = $modx->makeUrl($siblings[$nextkey]["id"]);
$nextChunk = $modx->getChunk("sibling-next", array("url"=>$nexturl,"title"=>$siblings[$nextkey]["title"]));
} else {
$nextChunk = $modx->getChunk("sibling-empty", array());
}
$modx->setPlaceholders(array('Next' => $nextChunk),'sibling.');
$modx->setPlaceholders(array('Prev' => $prevChunk),'sibling.');
@sepiariver
Copy link

what was the issue you were having with this @dubrod?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment