2010-01-08 44 views
11

Tôi đang dò tìm vòng lặp cho các liên kết Nav bên dưới bài đăng của tôi. Điều này sẽ chuyển thành _layout của posts.htmlGặp khó khăn với Jekyll/Liquid

Tôi không thể nhận liên kết để không hiển thị nếu bài đăng là bài đăng cuối cùng hoặc bài đăng đầu tiên. Bất cứ sự giúp đỡ nào cũng thật sự tuyệt vời.

 
{% for post in site.posts %} 

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

{% endfor %} 

Trả lời

21

Tôi có may mắn hơn sử dụng 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

Thay đổi báo cáo if để chỉ kiểm tra xem post.previous có tồn tại không.

{% 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

Tôi có may mắn hơn sử dụng 'page.next' và 'page.previous', nhưng nhờ sự giúp đỡ. –

0

Thêm hình ảnh vào nền liên kết của bạn. Đối với hình ảnh xuất hiện chỉ cần thêm image: [image location] trước YAML của bạn vấn đề

<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> 
Các vấn đề liên quan