我正在尝试为我的帖子下方的导航链接做一个循环。这是进入posts.html的_layout
我无法获取不显示帖子是最后一篇还是第一篇的链接。任何帮助都是极好的。
{% for post in site.posts %} {% if post.previous != forloop.last %} ← 最后 {% elsif post.next != forloop.first %} 下一个 → {% 万一 %} {% endfor %}
我有更好的运气使用 page.next/previous
{% if page.previous %}
<a rel="prev" href="{{ page.previous.url }}">← Older</a>
{% endif %}
{% if page.next %}
<a rel="next" href="{{ page.next.url }}">Newer →</a>
{% endif %}
更改您的if
陈述以检查是否post.previous
存在。
{% if post.previous %}
<span class="page-nav-item">
<a rel="prev" href="{{ post.previous.url }}" title="View {{ post.previous.title }}">← 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 →</a>
</span>
{% endif %}
将图像添加到您的链接背景。要显示图像,只需添加image: [image location]
您的 YAML 前端内容
<div class="postNav clearfix">
{% if page.previous.url %}
<a class="prev{% if page.previous.image %} image{% endif %}" href="{{ page.previous.url }}"><span>« {{ 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 }} »</span>
{% if page.next.image %}
<img src="{{ '/assets/blog-img/' | append: page.next.image }}" alt="">
{% endif %}
</a>
{% endif %}
</div>