Forum

Thread tagged as: Question, Configuration, Runway

How to get next / prev links in Runway collection detail view

I found an guide (https://docs.grabaperch.com/perch/content/functions/how-do-i-link-prev-next/) - on doing this for content regions but from my efforts so far it doesn't appear to work on collections - presumably due to the fact collections don't have _sortvalue.

My setup is like so: I have a list/detail page for a collection. Inside the detail view I would like quick links to the next or previous collection item in sequence. How could I determine the next in collection while inside the detail view. Any pointers if this is possible?

Mathew Doidge

Mathew Doidge 2 points

  • 4 years ago

Collections should still have _sortvalue. Have you tried <perch:showall /> to see what's available? I've used this on a few collections:

<?php 

// get current item
$result = perch_content_custom('Collections',[
    'filter'=>'slug',
    'match'=>'eq',
    'value'=>perch_get('s'),
    'skip-template'=>true,
    'return-html'=>true
]);

// output item in detail view
echo $result['html'];

// get previous item
$prev = perch_content_custom('Collections', array(
  'template' => 'collections/_prev.html',
  'filter' => '_order',
  'match' => 'lt',
  'value' => $result[0]['_sortvalue'],
  'sort' => '_order',
  'sort-order' => 'DESC',
  'count' => 1,
  'skip-template' => true,
  'return-html' => true
));
// if there is no previous item, loop to last item
if (empty(array_filter($prev))) {
    perch_content_custom('Collections', array(
      'template' => 'collections/_prev.html',
      'sort' => '_order',
      'sort-order' => 'DESC',
      'count' => 1
    ));
} 
// previous item link
else {
    echo $prev['html'];
}

// get next item
$next = perch_content_custom('Collections', array(
  'template' => 'collections/_next.html',
  'filter' => '_order',
  'match' => 'gt',
  'value' => $result[0]['_sortvalue'],
  'sort' => '_order',
  'sort-order' => 'ASC',
  'count' => 1,
  'skip-template' => true,
  'return-html' => true
));
// if no next item, loop to first item
if (empty(array_filter($next))) {
    perch_content_custom('Collections', array(
      'template' => 'collections/_next.html',
      'sort' => '_order',
      'sort-order' => 'ASC',
      'count' => 1
    ));
} 
// next item link
else {
    echo $next['html'];
}

_prev.html:

<a href="/collections/<perch:content id="slug" type="slug" />">Back</a>

_next.html:

<a href="/collections/<perch:content id="slug" type="slug" />">Next</a>

Hi Shane, I managed to get something working from that guide in the end, although your solution is an improvement over mine so I appreciate the code, I will implement this. Muchos love.

Glad it helped!