【问题标题】:Navigation for pages (not for posts) in JekyllJekyll 中的页面导航(不是帖子)
【发布时间】:2015-06-25 18:31:29
【问题描述】:

我正在使用 Jekyll 构建一个网站,只需要页面。
我想找到一种方法来为页面生成上一个和下一个链接,并为页面提供一个属性,如order。 有什么东西可以完成这项工作(没有插件)?我只能找到有关帖子的信息。

【问题讨论】:

    标签: pagination jekyll


    【解决方案1】:
    {% assign sortedPages = site.pages | sort:'order' | where: 'published', true %}
    
    {% for p in sortedPages %}
        {% if p.url == page.url %}
            {% if forloop.first == false %}
                {% assign prevIndex = forloop.index0 | minus: 1 %}
                <a href="{{site.baseurl}}{{sortedPages[prevIndex].url}}">
                    previous : {{sortedPages[prevIndex].title}}
                </a>
            {% endif %}
            {% if forloop.last == false %}
                {% assign nextIndex = forloop.index0 | plus: 1 %}
                <a href="{{site.baseurl}}{{sortedPages[nextIndex].url}}">
                    next : {{sortedPages[nextIndex].title}}
                </a>
            {% endif %}
        {% endif %}
    {% endfor %}
    

    这样就可以了。

    为了过滤您发布的页面,您可以在页面前端添加一个published 变量。

    设置变量

    published: true -> 这是一个布尔值

    published: 'true' -> 这是一个字符串

    使用 where 过滤器

    | where: 'published', true 将测试布尔值

    | where: 'published', 'true' 将测试字符串

    【讨论】:

    • 感谢大卫!但我不知道:在您的代码中添加了过滤页面的密钥,以及如何只有两个链接而不是我所有页面的链接。
    • 好的,我改了代码,还是不行,我的页面前面是这样的:--- layout: page title: Title of my page permalink: /part-01/title-of-my-page order: 12 published: true ---
    • 我已经编辑了我的答案,正确的过滤器是| where: 'published', true
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-22
    • 2014-10-16
    • 2013-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多