I've got a basic portfolio website which has "prev project" and "next project" buttons on each entry detail page. I'd like for when you reach the last project for the "next project" button to simply link back to the first instead of simply not appearing at all.

Any idea how this could be done? Thanks so much in advance.

 {% if nextEntry %}<a href="{{ nextEntry.url }}">Prev Project</a>{% endif %}
 {% if prevEntry %}<a href="{{ prevEntry.url }}">Next Project</a>{% endif %}

I'm going to assume your project section is named projects, if it's not, replace it with you section name.

Try this:

{% if nextEntry %}
    {% set nextEntryUrl = nextEntry.url %}
{% else %}
    {% set firstEntry = craft.entries.section('projects').first() %}
    {% set nextEntryUrl = firstEntry.url %}
{% endif %}

{% if prevEntry %}
    {% set prevEntryUrl = prevEntry.url %}
{% else %}
    {% set lastEntry = craft.entries.section('projects').last() %}
    {% set prevEntryUrl = lastEntry.url %}
{% endif %}

<a href="{{ prevEntryUrl }}">Prev Project</a>
<a href="{{ nextEntryUrl }}">Next Project</a>

Could you do something like (untested)

{% set nextUrl = nextEntry is defined and nextEntry ? nextEntry.url : url('portfolio/root') %}
<a href="{{ nextUrl }}">Next page</a>

Or, for probably better usability

 {% if nextEntry %}
  <a href="{{ nextEntry.url }}">Next Project</a>
 {% else %}
   <a href="{{ url('portfolio/root') }}">back to start</a>
 {% endif %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.