D
D
danil_linkins2017-09-06 16:56:16
PHP
danil_linkins, 2017-09-06 16:56:16

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>');
    }
}

In such a structure: The 014da1c4924441c183287caa5c88084b.png
question is whether the same navigation can be implemented only on the "necessary" pages without a parent, for example, by tag, or by id, but the number does not always go in order. Tell me please.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Ulo, 2017-09-06
@Salexer

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 question

Ask a Question

731 491 924 answers to any question