Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having difficulties with Jekyll / Liquid

Tags:

jekyll

liquid

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 %}
like image 342
Colby Olson Avatar asked Jan 08 '10 07:01

Colby Olson


2 Answers

I had better luck using page.next/previous

    {% if page.previous %} 
        <a rel="prev" href="{{ page.previous.url }}">&larr; Older</a>
    {% endif %}
    {% if page.next %} 
        <a rel="next" href="{{ page.next.url }}">Newer &rarr;</a>
    {% endif %}
like image 93
Colby Olson Avatar answered Oct 19 '22 23:10

Colby Olson


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 }}">&larr; 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 &rarr;</a>
</span>
{% endif %}
like image 28
Andrew Avatar answered Oct 19 '22 23:10

Andrew