I guess I should say that the notes controller and the prevnext snippet are independent of each other. I was simply stating that inside my notes controller I am able to sort the articles list by my 2 defined fields via the sortby method. The prevnext snippet was something from the kirby 2 starterkit I pulled over into kirby 3, then changed stuff that was depricated to their new names/functions.
I have a notes.php and a note.php. Notes is the all articles list page, note is the article page. Inside my notes.php it will obviously get all articles and sort them correctly by the 2 defined fields. My note.php is the article page that has a snippet of prevnext as listed above.
@jimbobrjames If you could propose a solution that would be great, I think for now I will have to look at using a custom page method as @texnixe has suggested.
notes.php (controller)
<?php
return function ($page) {
// Notes
$notes = $page->children()->listed();
$sorted = $notes->sortBy('noteDate', 'desc', 'noteTime', 'desc');
// Categories & Tags
$categories = $notes->pluck('category', ',', true);
$categoryurl = $page->url();
$tags = $notes->pluck('tags', ',', true);
if ($tag = urldecode(param('tag'))) {
$sorted = $sorted->filterBy('tags', $tag, ',');
}
if ($category = urldecode(param('category'))) {
$sorted = $sorted->filterBy('category', $category, ',');
}
sort($categories);
// Pagination
$note = $sorted->paginate(4);
$pagination = $note->pagination();
return [
'categories' => $categories,
'categoryurl' => $categoryurl,
'note' => $note,
'notes' => $notes,
'pagination' => $pagination,
'sorted' => $sorted,
'tags' => $tags
];
};
notes.php (templates)
<?php snippet('header') ?>
<main>
<div class="l-content">
<div class="o-intro">
<h1 class="o-intro__title"><?= $page->headline()->or($page->title()) ?></h1>
<p class="o-intro__description"><?= $page->intro() ?></p>
</div>
<?php if (count($categories)): ?>
<nav class="o-filter">
<ul class="o-filter__list">
<li class="o-filter__item"><a href="<?= $categoryurl ?>" class="o-filter__link">All</a></li>
<?php foreach ($categories as $category): ?>
<li class="o-filter__item">
<a href="<?= url($categoryurl, ['params' => ['category' => urlencode($category)]]) ?>" class="o-filter__link">
<?= $category ?>
</a>
</li>
<?php endforeach ?>
</ul>
</nav>
<?php endif ?>
<div class="o-text">
<?= $page->editor()->blocks() ?>
</div>
</div>
<div class="o-list">
<?php foreach ($note as $item): ?>
<article class="o-note">
<a href="<?= $item->url() ?>" class="o-note__link">
<?php if ($cover = $item->cover()->toFile()): ?>
<img src="<?= $cover->thumb(['blur' => 100, 'q' => 10, 'fit' => 'crop', 'fp-x' => $cover->focusX(), 'fp-y' => $cover->focusY()])->url() ?>" data-src="<?= $cover->thumb(['fit' => 'crop', 'fp-x' => $cover->focusX(), 'fp-y' => $cover->focusY()])->url() ?>" alt="<?= $cover->alt() ?>" class="o-note__img j-lazy">
<?php endif ?>
<div class="o-note__info">
<time class="o-note__date"><?= $item->noteDate()->toDate('%d %h %Y') ?></time>
<div class="o-note__text">
<p class="o-note__category"><?= $item->category() ?></p>
<h2 class="o-note__title"><?= $item->headline()->or($item->title())->excerpt(40) ?></h2>
</div>
<p class="o-note__read"><?= t('readmore') ?></p>
</div>
</a>
</article>
<?php endforeach ?>
</div>
<?php snippet('pagination') ?>
</main>
<?php snippet('footer') ?>
note.php (controller)
<?php
return function ($page) {
// Images
$cover = $page->cover()->toFile();
// Tags
$tags = $page->tags()->split(',');
$tagurl = $page->parent()->url();
if ($tag = urldecode(param('tag'))) {
$tags = $page->filterBy('tags', $tag, ',');
}
// Related Articles
$related = page('blog')->children()->listed()->filterBy('category', $page->category(), ',');
$sorted = $related->sortBy('noteDate', 'desc', 'noteTime', 'desc')->not($page->uri())->limit(2);
$count = $sorted->count();
if ($count < 2) {
$latest = page('blog')->children()->listed()->not($sorted);
$sortlatest = $latest->sortBy('noteDate', 'desc', 'noteTime', 'desc');
$sorted = $sorted->add($sortlatest)->not($page->uri())->limit(2);
}
return [
'cover' => $cover,
'related' => $related,
'sorted' => $sorted,
'tags' => $tags,
'tagurl' => $tagurl
];
};
note.php (templates)
<?php snippet('header') ?>
<main>
<div class="l-content">
<div class="o-intro">
<h1 class="o-intro__title"><?= $page->headline()->or($page->title()) ?></h1>
<p class="o-intro__description"><?= $page->intro() ?></p>
</div>
<?php if (count($tags)): ?>
<nav class="o-filter">
<ul class="o-filter__list">
<?php foreach($tags as $tag): ?>
<li class="o-filter__item">
<a href="<?= url($tagurl, ['params' => ['tag' => urlencode($tag)]]) ?>" class="o-filter__link">
<?= $tag ?>
</a>
</li>
<?php endforeach ?>
</ul>
</nav>
<?php endif ?>
</div>
<?php if ($cover): ?>
<figure class="o-cover">
<img src="<?= $cover->thumb(['blur' => 100, 'q' => 10, 'fit' => 'crop', 'fp-x' => $cover->focusX(), 'fp-y' => $cover->focusY()])->url() ?>" data-src="<?= $cover->thumb(['fit' => 'crop', 'fp-x' => $cover->focusX(), 'fp-y' => $cover->focusY()])->url() ?>" alt="<?= $cover->alt() ?>" class="o-cover__img j-lazy">
</figure>
<?php endif ?>
<div class="l-content">
<div class="o-text">
<?= $page->editor()->blocks() ?>
</div>
<p class="o-author">
Posted on <time class="o-author__date"><?= $page->noteDate()->toDate('%d %B %Y') ?></time> in <a href="<?= url($page->parent()->url(), ['params' => ['category' => urlencode($page->category())]]) ?>" class="o-filter__link"><?= ucwords($page->category()) ?></a> <?php if ($author = $page->author()->toUser()): ?>by <?= $author->name() ?><?php endif ?>
</p>
</div>
<?php snippet('prevnext') ?>
<div class="o-list">
<?php foreach($sorted as $item): ?>
<article class="o-note">
<a href="<?= $item->url() ?>" class="o-note__link">
<?php if ($cover = $item->cover()->toFile()): ?>
<img src="<?= $cover->thumb(['blur' => 100, 'q' => 10, 'fit' => 'crop', 'fp-x' => $cover->focusX(), 'fp-y' => $cover->focusY()])->url() ?>" data-src="<?= $cover->thumb(['fit' => 'crop', 'fp-x' => $cover->focusX(), 'fp-y' => $cover->focusY()])->url() ?>" alt="<?= $cover->alt() ?>" class="o-note__img j-lazy">
<?php endif ?>
<div class="o-note__info">
<time class="o-note__date"><?= $item->noteDate()->toDate('%d %h %Y') ?></time>
<div class="o-note__text">
<p class="o-note__category"><?= $item->category() ?></p>
<h2 class="o-note__title"><?= $item->headline()->or($item->title())->excerpt(40) ?></h2>
</div>
<p class="o-note__read"><?= t('readmore') ?></p>
</div>
</a>
</article>
<?php endforeach ?>
</div>
</main>
<?php snippet('footer') ?>
prevnext.php (snippet)
Listed above