首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Jekyll -具有不同类别的下一个和前一个链接

Jekyll -具有不同类别的下一个和前一个链接
EN

Stack Overflow用户
提问于 2015-02-05 05:33:52
回答 2查看 3.1K关注 0票数 2

我正在使用Jekyll建立一个博客,我成功地在我的布局页面上实现了下一个和前一个链接(使用this answer)。我已经添加了几个类别(使用this answer),每个页面都会遍历适当的类别,只显示我希望它们显示的内容。

我现在唯一的问题是,上一个和下一个按钮仍然遍历所有的帖子,而不管它们属于什么类别。

这是我在布局底部的下一个和前一个链接中使用的代码:

代码语言:javascript
复制
        {% 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 %}

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

EN

回答 2

Stack Overflow用户

发布于 2015-02-05 12:25:59

经过一些研究之后。我在这篇博文上找到了我想要的-- http://ajclarkson.co.uk/blog/jekyll-category-post-navigation/

只需要在_plugin目录中添加一个插件,并在其中添加以下代码:

代码语言:javascript
复制
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即可。

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

票数 6
EN

Stack Overflow用户

发布于 2015-05-04 15:41:47

如果你能够安装插件的话,ajclarkson插件在分类中进行分页是一个正确而有用的解决方案。

如果你想部署到Github页面,你不能使用插件。

我使用此方法在类别中进行分页。这确保了来自不同类别的帖子永远不会出现:

代码语言:javascript
复制
{% 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 %}

请注意,如果此解决方案必须跳过不同类别的内容,它将无法找到下一篇文章。它避免了最坏的情况(?)场景,它链接到一些无意的/并不是真正的帖子。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28331879

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档