I'm tring to do a loop for Nav links below my posts. This is going into the _layout of posts.html
I can't get the link to not show if the post is the last or the first. Any help would be awesome.
{% for post in site.posts %} {% if post.previous != forloop.last %} ← Last {% elsif post.next != forloop.first %} Next → {% endif %} {% endfor %}
I had better luck using page.next/previous
{% if page.previous %}
<a rel="prev" href="{{ page.previous.url }}">← Older</a>
{% endif %}
{% if page.next %}
<a rel="next" href="{{ page.next.url }}">Newer →</a>
{% endif %}
Change your if
statements to just check if post.previous
exists.
{% if post.previous %}
<span class="page-nav-item">
<a rel="prev" href="{{ post.previous.url }}" title="View {{ post.previous.title }}">← View previous article</a>
</span>
{% endif %}
{% if post.next %}
<span class="page-nav-item">
<a rel="next" href="{{ post.next.url }}" title="View {{ post.next.title }}">View next article →</a>
</span>
{% endif %}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With