与Jekyll / Liquid有困难

时间:2010-01-08 07:08:09

标签: liquid jekyll

我想在我的帖子下面为Nav链接做一个循环。这是进入posts.html的_layout

如果帖子是最后一个或第一个,我无法获得不显示的链接。任何帮助都会很棒。

{% for post in site.posts %}

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

{% endfor %}

3 个答案:

答案 0 :(得分:22)

我使用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 :(得分: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 %}

答案 2 :(得分: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> 
相关问题