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开发者_JS百科 {% 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 %}
Add images to your link background.
For images to show up just add image: [image location]
in your YAML front matter
<div class="postNav clearfix">
{% if page.previous.url %}
<a class="prev{% if page.previous.image %} image{% endif %}" href="{{ page.previous.url }}"><span>« {{ page.previous.title }}</span>
{% if page.previous.image %}
<img src="{{ '/assets/blog-img/' | append: page.previous.image }}" alt="">
{% endif %}
</a>
{% endif %}
{% if page.next.url %}
<a class="next{% if page.next.image %} image{% endif %}" href="{{ page.next.url }}"><span>{{ page.next.title }} »</span>
{% if page.next.image %}
<img src="{{ '/assets/blog-img/' | append: page.next.image }}" alt="">
{% endif %}
</a>
{% endif %}
</div>
精彩评论