在Jekyll中循环上一篇和下一篇文章

时间:2015-04-29 01:07:40

标签: jekyll

我基于Jekyll的投资组合目前按时间顺序列出项目。我使用page.previouspage.next来显示我的投资组合中的下一个和上一个帖子。如果没有以前的'或者' next'项目,那里有一个巨大的空地。

我该如何修改:

<section id="more-work">
    {% if page.previous %}
    <div class="left">
        <a class="equalize" href="{{page.previous.url}}" title="Previous Post: {{page.previous.title}}">
            <div>
                <h6> ← Previous Project </h6>
                <h4> {{page.previous.title}} </h4>
            </div>
        </a>
    </div>
    {% endif %} {% if page.next %}
    <div class="right">
        <a class="equalize" href="{{page.next.url}}" title="next Post: {{page.next.title}}">
            <div>
                <h6> Next Project →</h6>
                <h4> {{page.next.title}}</h4>
            </div>  
        </a>
    </div>
    {% endif %}
</section>

...所以它循环到列表的末尾或列表的开头?

1 个答案:

答案 0 :(得分:5)

这可以完成这项工作:

<section id="more-work">
    {% if page.previous %}
        {% assign previous = page.previous %}
    {% else %}
        {% assign previous = site.posts[0] %}
    {% endif %}
    <div class="left">
        <a class="equalize" href="{{site.baseurl}}{{previous.url}}" title="Previous Post: {{previous.title}}">
            <div>
                <h6> ← Previous Project </h6>
                <h4> {{previous.title}} </h4>
            </div>
        </a>
    </div>

    {% if page.next %}
        {% assign next = page.next %}
    {% else %}
        {%comment%}
           As the array index start at zero we substract 1 to the total count,
           we then get the last post index in the site.posts array
        {%endcomment%}
        {% assign last lastPostIndex = site.posts | size | minus: 1 %}
        {% assign next = site.posts[lastPostIndex] %}
    {% endif %}
    <div class="right">
        <a class="equalize" href="{{site.baseurl}}{{next.url}}" title="next Post: {{next.title}}">
            <div>
                <h6> Next Project →</h6>
                <h4> {{next.title}}</h4>
            </div>
        </a>
    </div>
</section>
相关问题