html - Jekyll - 不同类别的下一个和上一个链接

标签 html jekyll

我正在使用 Jekyll 构建博客,我成功地在我的布局页面上实现了下一个和上一个链接(使用 this answer)。我添加了几个类别(使用 this answer )并且每个页面都遍历正确的类别,只显示我想要的内容。

我现在唯一的问题是 Previous 和 Next 按钮仍然遍历所有帖子,无论它们属于哪个类别。

这是我用于布局底部的下一个和上一个链接的代码:

        {% 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 %}

有没有办法让“下一篇”和“上一篇”按钮转到当前帖子类别中的下一篇或上一篇帖子?

最佳答案

经过一番研究。我在这篇博文中找到了我要找的东西 - 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.next_in_category.url 和 page.previous_in_category.url。

我希望这对遇到同样问题的人有所帮助。

关于html - Jekyll - 不同类别的下一个和上一个链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28331879/

相关文章:

html - 父级填充宽度为 100% 的子级

html - 右对齐列表项

javascript - 单击按钮显示内容时如何隐藏按钮?

html - 具有第一个/li 标记文本 "Something"的 div 中所有//a[@href] 标记的 XPath

linux - WSL2 Docker Linux 卷权限问题

html - 如何在 Rmarkdown html 输出中有一个 float 图像?

html - 如何去除 float 标签?

Github 页面 : How to extend base layout?

jekyll - 如何将旧的Netlify + Jekyll站点地址重定向到新地址?

html - 自动为图片标签添加宽高属性