I want to use prev and next buttons to navigate through my pages (but i want them to sort by the date-field which is in my template). But this doesn’t work for me.
<?php if($prev = $page->prev('date', 'desc')): ?>
<a href="<?= $prev->url() ?>">previous page</a>
<?php endif ?>
The prev()
and next()
methods do not accept a parameter.
I created custom methods for this purpose:
foreach($this->index() as $p) {
$depth[] = $p->depth();
}
return max($depth);
}
return null;
},
'canonicalUrl' => function() {
return $this->url() . r(params() || $this->isHomePage(), '/') . kirby()->request()->params();
},
'getPrev' => function($siblings, $sort = [], $status = false) {
if($sort) $siblings = call(array($siblings, 'sortBy'), $sort);
$index = $siblings->indexOf($this);
if($index === false or $index === 0) return null;
if($status) {
$siblings = $siblings->limit($index);
$siblings = $siblings->{$status}();
return $siblings->last();
} else {
return $siblings->nth($index - 1);
}
Hi, thanks,
what’s the syntax i need to use?
if($prev = $page->getPrev('date, desc')){
...
No, you need to pass the sorted collection
sorry, i must be doing something wrong. I get an error that $prev is null.
$collection = $page->parent()->children()->listed()->sortBy('date','desc');
$prev=$page->getPrev($collection);
echo $prev->title();
I have to look into later, don’t have the time right now…
I made it work, you can close this issue.