我正在使用Jekyll建立一个博客,我成功地在我的布局页面上实现了下一个和前一个链接(使用this answer)。我已经添加了几个类别(使用this answer),每个页面都会遍历适当的类别,只显示我希望它们显示的内容。
我现在唯一的问题是,上一个和下一个按钮仍然遍历所有的帖子,而不管它们属于什么类别。
这是我在布局底部的下一个和前一个链接中使用的代码:
{% 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 %}有没有办法让“下一篇”和“上一篇”按钮转到当前帖子类别中的下一篇或上一篇文章?
发布于 2015-02-05 12:25:59
经过一些研究之后。我在这篇博文上找到了我想要的-- 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在超文本标记语言中不用使用page.next.url和page.prev.url,只需使用page.next_in_category.url和page.previous_in_category.url即可。
我希望这对任何遇到同样问题的人都有所帮助。
发布于 2015-05-04 15:41:47
如果你能够安装插件的话,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 %}请注意,如果此解决方案必须跳过不同类别的内容,它将无法找到下一篇文章。它避免了最坏的情况(?)场景,它链接到一些无意的/并不是真正的帖子。
https://stackoverflow.com/questions/28331879
复制相似问题