Forum

Thread tagged as: Question

Trouble with next/previous links in list/detail setup

I have a one page list/detail setup and have followed the instructions here for creating next/previous links.

https://solutions.grabaperch.com/architecture/how-do-i-link-prev-next.html

Problem: However, only the Next link shows up. It works, but "Previous" never appears.

My list/detail page code: <?php include('../perch/runtime.php'); ?>

<?php perch_layout('header', array( 'title'=>'Work', 'class'=>'work', )); ?>

<?php

 perch_content_create('Work', array(
      'template'  => 'work_detail.html',
      'multiple'  => true,
      'edit-mode' => 'listdetail',
 ));


 if (perch_get('s')) {




      // Detail mode
      $result = perch_content_custom('Work', array(
           'template' => 'work_detail.html',
           'filter'   => 'slug',
           'match'    => 'eq',
           'value'    => perch_get('s'),
           'count'    => 1,
           'skip-template' => true,
           'return-html' => true,
      )); 

      echo $result['html'];

      perch_content_custom('Work', array(
        'template'   => 'work_prevnext.html',
        'filter'     => '_order',
        'match'      => 'gt',
        'value'      => $result[0]['_sortvalue'],
        'sort'       => '_order',
        'sort-order' => 'ASC',
        'count'      => 1,
    ));

    PerchSystem::set_var('is_prev', true);

    perch_content_custom('Products', array(
        'template'   => 'work_prevnext.html',
        'filter'     => '_order',
        'match'      => 'lt',
        'value'      => $result[0]['_sortvalue'],
        'sort'       => '_order',
        'sort-order' => 'DESC',
        'count'      => 1,
    ));

 } else {

      // List mode
      perch_content_custom('Work', array(
           'template' => 'work_listing.html',
      )); 
 }

?>

<?php perch_layout('footer'); ?>

My work_prevnext.html template code:

<a href="?s=<perch:content id="slug" type="slug" />"> <perch:if exists="is_prev">Previous<perch:else />Next</perch:if> </a>

Also one final question: Once it's working, how can the next/previous links be placed somewhere other than at the bottom, after the output of the detail content? If a visitor wants to browse through all the detail pages quickly, it's makes more sense for the UI to be higher up on the page, preferably above the output.

Thanks!

Franz Neumann

Franz Neumann 0 points

  • 5 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

The 'previous' link is querying Products and not Work.

You can place the results anywhere you wish on the page.

Thanks Drew. I got the next/previous links working, but I'm still having a couple of issues.

1) I can't seem to get the previous and next links to appear as "‹ Previous Next ›". Instead, they appear as "Next › ‹ Previous." I tried switching my work_prevnext template sections around, but that just doubled up the previous/next links. Here's my template code for the previous/next links:

<a href="?s=<perch:content id="slug" type="slug" />"> <perch:if exists="is_prev">‹ Previous<perch:else /> <perch:content id="title" type="text" /> </a>

<a href="?s=<perch:content id="slug" type="slug" />"> Next ›</perch:if> <perch:content id="title" type="text" /> </a>

2) The other issue I have is how do I get these previous/next links ABOVE the content output by the detail page. I tried moving up the code on my detail page and while the links DID show up, they next link points to the same page one's on, and the previous link only works for one click. I'm sure it has to do with the whole skip-template, echo $result order, but I'm not sure how to resolve it. Here's my detail.php page:

<?php include('../perch/runtime.php'); ?>

<?php perch_layout('header', array(
    'title'=>'Work',
    'class'=>'work',
));
?>


 <?php 
     $result = perch_content_custom('Work', array(
     'page' => '/work/index.php',
     'template' => 'work_detail.html',
     'filter' => 'slug',
     'match' => 'eq',
     'value' => perch_get('s'),
     'count' => 1,
     'skip-template' => true,
     'return-html' => true,
));
?>

<?php
    echo $result['html'];
?>

<div class="row">
    <div class="small-12 medium-10 medium-offset-1 columns">
<?php
perch_content_custom('Work', array(
    'page' => '/work/index.php',
    'template'   => 'work_prevnext.html',
    'filter'     => '_order',
    'match'      => 'gt',
    'value'      => $result[0]['_sortvalue'],
    'sort'       => '_order',
    'sort-order' => 'ASC',
    'count'      => 1,
)); 
?>

<?php
    PerchSystem::set_var('is_prev', true);

perch_content_custom('Work', array(
    'page' => '/work/index.php',
    'template'   => 'work_prevnext.html',
    'filter'     => '_order',
    'match'      => 'lt',
    'value'      => $result[0]['_sortvalue'],
    'sort'       => '_order',
    'sort-order' => 'DESC',
    'count'      => 1,
)); 

?>
    </div>
</div>


<?php perch_layout('footer'); ?>

Thanks!

Drew,

I still have issue #2, but I solved issue #1 via (https://forum.grabaperch.com/forum/11-20-2014-next-and-previous-items).

Drew McLellan

Drew McLellan 2638 points
Perch Support

The main content is output with this line:

echo $result['html'];

So you can move that further down the page if you need to.

Many thanks. Your support is why (among other things) I use Perch for all my clients.