【问题标题】:Jekyll truncate number of blog posts conditional on excluding someJekyll 截断博客文章的数量,条件是排除一些
【发布时间】:2017-08-14 06:55:27
【问题描述】:

在我的 Jekyll 网站上,我有一个概览页面,我在上面列出了我最近的 10 篇博文。

不过,我还为我的一些博文和我不想展示的博文分配了一个标签 exclude。这可行,但我没有得到最后 10 篇博文,而是 10 减去 exclude 博文的数量。

看起来是这样的:

---
layout: page
title: "Last posts"
permalink: /last/
---

### Last posts

{% for post in site.posts limit:10 %}
    {% unless post.category == "exclude"%}
      * {{ post.date | date_to_string }} » [ {{ post.title }} ]({{ post.url }})
    {% endunless %}
{% endfor %}

如何始终显示最后 10 个非exclude 博客帖子?

【问题讨论】:

    标签: arrays jekyll limit blogs liquid


    【解决方案1】:

    显示最近 10 篇非排除博客文章:

    1. 创建一个包含不包含 exclude 标记的帖子的数组。

      {% assign nonexcludeposts = ''|split:''%}
      {% for post in site.posts %}
          {% unless post.category == "exclude"%}
          {% assign nonexcludeposts = nonexcludeposts|push:post%}
          {% endunless %}
      {% endfor %}
      
    2. 显示 10 个最新帖子

      <ul>
      {% for post in nonexcludeposts limit:10 %}
            <li>
            <a href="{{post.url|absolute_url}}">{{post.title}}</a>
            </li>
      {% endfor %}
      </ul>
      

    【讨论】:

    • 它会提取帖子和正确数量的帖子,但此列表无法正确呈现给我。也许我把这个包括错了。然后在页面上显示:* 2017 年 8 月 8 日 » [我的好帖子](/2017/08/08/my-great-post/) * 2017 年 7 月 28 日 » [另一个好帖子](/2017/07/28/another-good-poost/) ... [其他帖子] ...
    • 我在该部分使用了原始代码,因为它与问题无关,为每个帖子生成超链接,将其替换为&lt;a href="{{post.url|absolute_url}}"&gt;{{post.title}}&lt;/a&gt;
    猜你喜欢
    • 2015-12-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多