Forum

Thread tagged as: Question

Prev and Next links

Hello. I've successfully implemented this:

https://docs.grabaperch.com/perch/content/functions/how-do-i-link-prev-next/

I need the list to loop, so that when the last project in the list is displayed, the next link loops back to the first.

Is this posible?

Thanks.

Tim Rex

Tim Rex 0 points

  • 4 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, if there's no next item, output the link to the first. The first is always a known URL.

Tim Rex

Tim Rex 0 points

Thanks. I'm not sure how I do that using this template:

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

It needs to work for the Next & Prev links.

Drew McLellan

Drew McLellan 2638 points
Perch Support

What you need to do is if there's no next link, link to the first item.

Tim Rex

Tim Rex 0 points

Thanks Drew. I understand what you're saying, but I'm not sure how I go about implementing it?

The 'Next' button need to go to the first item if there no next item. The 'Prev' button needs to go to the last item if there's no prev item.

I'm drawing a blank on how I need to set up the template and page. Sorry.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Do you know how to filter a region to get the first result?

Tim Rex

Tim Rex 0 points

No idea.

I'm guessing I need to do something like this:

<?php

if *Look to see if there is a next item* {

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

}

else {

code here to display the first item

}

?>

Any help appreciated.

Tim Rex

Tim Rex 0 points

Hi.

I think this is what I need for the 'Next' link, but I don't know what the 'if' logic should be.

Can you help please?

<?php

if ??????????? {

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

}

else {

perch_content_custom( 'Work Project', array(
'template' => 'product_prevnext.html',
'count' => 1,
) );

}

?>

Thank you.

Drew McLellan

Drew McLellan 2638 points
Perch Support

This is beyond the scope of Perch support but I'll leave it here in case someone else wants to help.