Collection:next / previous with Slugs? #14244
-
|
Hi all, I have what might be a specific use-case that I am struggling with and would appreciate some guidance. For context, I am more of a front-end person and have very little knowledge/experience with php. I am also still new to Statamic as I just built the site in question last month after migrating from Jekyll. That all to say, maybe there is an easy solution I am missing but hence the post. So, I have a collection of entries which need to be in a specific order so that users can cycle through them. Naturally I started with a collection and made it 'orderable' and then I would drag-and-drop entries as needed. But now with over 300 entries moving them around manually is simply not practical. This would become likely impossible as the catalog grows because of the 500 per page limit in the control panel. Therefore I am looking for a new method to create the order I need and retain current functionality. And I cannot sort by date because the dates are all over the place as entries are not made in order they need to be displayed in. Here is an idea of the collection: By updating the slugs to be Cardiovascular 01 for example I was able to order the entries in the way that I want and then not have issues as categories hit 20, 30, 40+ entries. On the home page, I have a section of the site where I am displaying all of the entries but in their respective categories and to get them ordered correctly, without manually ordering the entries in the control panel, I was able to do this: {{ collection:step2 title:contains="{{ slot }}" sort="slug:asc"}} {{ /collection:step2 }} Now, the problem I am having is on the individual page for each entry. In the previous iteration, when I was using an ordered collection, I was able to very simply use the collection:previous and collection:next tags to move between pages. However, that is not working now because the collection is un-ordered, and as mentioned dates are useless. What options do I have to regain this functionality? Is there a way to create something similar to move between entries with the collection sorted by Slug? The current version of the project is live here and on this page you can see the current functionality since the collection is still ordered. Thank you in advance! Of course I am happy to provide any additional info that would be useful. Hopefully this is easy and just my lack of knowledge/experience. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 9 replies
-
|
You should be able to use |
Beta Was this translation helpful? Give feedback.
-
|
We wrote a custom tag for a similar feature. It figures out the sort order of the collection and fetches the prev/next entry. It works great for unique values like {{ prev = { sibling:previous } }}
<a href="{{ url }}">Previous page: {{ title }}</a>
{{ /prev }}
{{ next = { sibling:next } }}
<a href="{{ url }}">Next page: {{ title }}</a>
{{ /next }}namespace App\Tags;
use Statamic\Facades\Entry as Entries;
use Statamic\Tags\Tags;
class Sibling extends Tags
{
public function next()
{
return $this->sibling(true);
}
public function previous()
{
return $this->sibling(false);
}
protected function sibling(bool $next = true)
{
$entry = Entries::find($this->params->get('current', $this->context->get('id')));
$collection = $entry->collection();
$column = $collection->sortField();
$dir = $collection->sortDirection();
$dir = $next ? $dir : ($dir === 'asc' ? 'desc' : 'asc');
$op = $dir === 'asc' ? '>=' : '<';
return Entries::query()
->whereStatus('published')
->where('collection', $collection->handle())
->where('site', $entry->locale())
->where('id', '!=', $entry->id())
->where($column, $op, $entry->value($column))
->orderBy($column, $dir)
->first();
}
} |
Beta Was this translation helpful? Give feedback.

We wrote a custom tag for a similar feature. It figures out the sort order of the collection and fetches the prev/next entry. It works great for unique values like
slugortitle.{{ prev = { sibling:previous } }} <a href="{{ url }}">Previous page: {{ title }}</a> {{ /prev }} {{ next = { sibling:next } }} <a href="{{ url }}">Next page: {{ title }}</a> {{ /next }}