在 Jekyll 中循环上一个和下一个 Post

Looping Previous and Next Post in Jekyll

我基于 Jekyll 的投资组合目前按时间顺序列出项目。我正在使用 page.previouspage.next 来显示我投资组合中的下一篇和上一篇文章。当没有 'previous' 或 'next' 项目时,就会有一个巨大的空白 space。

如何修改:

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

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

这可以完成工作:

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