我正在使用Jekyll构建博客,我成功地在我的布局页面上实现了Next和Previous链接(使用这个答案)。我添加了几个类别(使用这个答案),每个页面都会遍历适当的类别,只显示我想要的内容
我现在唯一的问题是"上一个"one_answers"下一个"按钮仍然会遍历所有帖子,无论它们属于哪个类别
这是我用于布局底部的下一个和上一个链接的代码:
{% if page.previous %}
<span class="previous-link">
<a rel="prev" href="{{ page.previous.url }}">Previous Entry</a>
</span>
{% endif %}
{% if page.next %}
<span class="next-link">
<a rel="next" href="{{ page.next.url }}">Next Entry</a>
</span>
{% endif %}
有没有办法让"下一篇"one_answers"上一篇"按钮转到当前帖子类别中的下一篇或上一篇帖子?
经过一番研究。我在这篇博客文章中找到了我想要的东西——http://ajclarkson.co.uk/blog/jekyll-category-post-navigation/
只需要在_plugin目录中添加一个插件,并在其中添加以下代码:
module Jekyll
class WithinCategoryPostNavigation < Generator
def generate(site)
site.categories.each_pair do |category, posts|
posts.sort! { |a,b| b <=> a}
posts.each do |post|
index = posts.index post
next_in_category = nil
previous_in_category = nil
if index
if index < posts.length - 1
next_in_category = posts[index + 1]
end
if index > 0
previous_in_category = posts[index - 1]
end
end
post.data["next_in_category"] = next_in_category unless next_in_category.nil?
post.data["previous_in_category"] = previous_in_category unless previous_in_category.nil?
end
end
end
end
end
而不是在HTML中使用page.next.url和page.prev.url,只需使用page.nxt_in_category.url和page.previous_in_categary.url。
我希望这能帮助任何遇到同样问题的人。
如果您能够安装插件,那么用于分类内分页的ajclarkson插件是一个正确而有用的解决方案。
如果你想部署到Github页面,你不能使用插件。
我使用这种方法在类别内进行分页。这确保了来自不同类别的帖子永远不会出现:
{% if page.next and page.next.categories contains "blog" %}
<a href="{{ page.next.url }}">Next Post</a>
{% else if page.previous and page.previous.categories contains "blog" %}
<a href="{{ page.previous.url }}">Previous Post</a>
{% endif %}
请注意,如果必须跳过不同类别的内容,此解决方案将找不到下一篇文章。它避免了最坏的(?)情况,即链接到一些无意/非故意的帖子。