How can I check whether the post I am in is the first or the last?

Hi, I am I want to make the next news part, if the user is in the first post, it should show the next post, if the user is in the last post, it should show the next post. Otherwise it should always show the next post.

The code I found in the documentation did not fully meet my needs:

image

And I tried something like this:

image

How can I tell if the post is the first or the last?

Not tested, but I’d try something like
{{#next_post}}
{{#if url}}
– do all the stuff you want to do if the post exists
{{/if}}
{{/next_post}}

That way you get into the next post context if it exists, then check that the url exists in that context, because if it doesn’t, you don’t want to render that section anyway.

If that doesn’t meet your needs, please post more about what you’re trying to do?

Thank you for your response,

What I’m trying to do is, if the user is on the latest post, suggest the previous one; otherwise, suggest the next post.

I think I’d use CSS to solve this problem. Show both the next and prev links, but hide the second when there’s two:

<div class="next-and-previous-links">
{{#next_post}}
   <a href="{{url}}">next</a>
{{/next_post}}

{{#prev_post}}
   <a href="{{url}}">prev</a>
{{/prev_post}}
</div>
.next-and-previous-links a + a {
   display: none;
}
2 Likes

Yes, this could work, thank you.