2010-01-08 119 views
11

我正在爲我的帖子下方的導航鏈接做一個循環。這是進入posts.html的_layout在Jekyll/Liquid遇到困難

我不能得到的鏈接不顯示,如果帖子是最後或第一。任何幫助都是極好的。

 
{% for post in site.posts %} 

    {% if post.previous != forloop.last %} 
     ← Last 
    {% elsif post.next != forloop.first %} 
     Next → 
    {% endif %} 

{% endfor %} 

回答

21

我使用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 %} 
1

更改您的if語句以檢查是否存在post.previous

{% 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 %} 
+0

我用「page.next」和「page.previous」有更好的運氣,但感謝您的幫助。 – 2010-01-11 01:52:16

0

圖像添加到您的鏈接背景有更好的運氣。 對於圖像顯示只是在你的YAML前面加image: [image location]

<div class="postNav clearfix"> 
    {% if page.previous.url %} 
     <a class="prev{% if page.previous.image %} image{% endif %}" href="{{ page.previous.url }}"><span>&laquo;&nbsp;{{ 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 }}&nbsp;&raquo;</span> 
     {% if page.next.image %} 
     <img src="{{ '/assets/blog-img/' | append: page.next.image }}" alt=""> 
     {% endif %} 
     </a> 
     {% endif %} 
</div>