Answer the question
In order to leave comments, you need to log in
Page navigation by Next and Prev?
Now navigation to neighboring pages is based on the parent and neighboring elements. That is, navigation only works relative to the children of the parent element.
PHP code:
<?php
$id = $modx->resource->get('id');
$parentId = $modx->resource->get('parent');
// $children = $modx->getChildIds($parentId,1,array("published"=>1));
$order = "ORDER BY menuindex asc";
$wh=" where published=1 and parent = $parentId ";
$table = 'modx_site_content';
$sql = "SELECT id FROM `$table` ".$wh." ".$order;
$query = new xPDOCriteria($modx, $sql, array());
$children = array();
if ($query->prepare() && $query->stmt->execute()){
$res = $query->stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($res as $row){
$children[]=$row["id"];
}
}
// echo "<pre>";
// print_r($children);die;
if (count($children)>0) {
$key = -1;
while($key == -1){
$key = array_search($id,$children);
}
$next = (($key+1) == count($children) ? -1 :($key+1));
$prev = $key-1;
if ($next>=0) {
$object = $modx->getObject('modResource', $children[$next]);
$modx->setPlaceholder('next','<p class="title-next"><a href="/'.$modx->makeUrl($object->get('id')).'">'.$object->get('pagetitle').'</a></p><a href="/'.$modx->makeUrl($object->get('id')).'" class="next"><em></em></a>');
} else {
$object = $modx->getObject('modResource', $children[$next]);
$modx->setPlaceholder('next','<p class="title-next"></p>');
}
if ($prev>=0) {
$object = $modx->getObject('modResource', $children[$prev]);
$modx->setPlaceholder('prev','<a href="/'.$modx->makeUrl($object->get('id')).'" class="prev"><em></em></a><p class="title-prev"><a href="/'.$modx->makeUrl($object->get('id')).'">'.$object->get('pagetitle').'</a></p>');
} else {
$object = $modx->getObject('modResource', $children[$next]);
$modx->setPlaceholder('prev','<p class="title-prev"></p>');
}
}
Answer the question
In order to leave comments, you need to log in
You can slightly correct the query to the database and you can also by tags, or by name ...
As I understand it, how this snippet works, you have no idea?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question