【问题标题】:Jekyll: only show recent posts excluding some catogariesJekyll:只显示最近的帖子,不包括某些类别
【发布时间】:2017-07-28 03:43:55
【问题描述】:

我是 Jekyll 的新手。我知道 Jekyll 支持使用以下代码显示最近的帖子:

<div id="recent-posts" class="recent-posts">
  {% for this_post in paginator.posts %}
  <div class="post">
      <a href="{{ this_post.url }}">
        <h2 class="post-title">{{ this_post.title }}</h2>
      </a>
      {% include category_info %}
      {% include source_info %}
  </div>
  {% endfor %}
</div>

但我不想显示帖子类别,说类别名称是"notshowing"。我怎样才能做到?

【问题讨论】:

    标签: jekyll liquid


    【解决方案1】:

    为避免显示特定类别,您可以使用unless 过滤器:

    仅在不满足特定条件(即 是,如果结果为假)。

    例如,在for 循环内,使用{% unless post.categories contains "notshowing"%}

    在您的示例中,使用网站帖子 site.posts 而不是 paginator.posts(您可以调整它以适应您的需要)它看起来像:

    <div id="recent-posts" class="recent-posts">
      {% for post in site.posts %}
      {% unless post.categories contains "notshowing"%}
      <div class="post">
          <a href="{{ post.url }}">
            <h2 class="post-title">{{ post.title }}</h2>
          </a>
      </div>
      {% endunless %}
      {% endfor %}
    </div>
    

    【讨论】:

    • 这项工作做得很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-19
    • 2018-07-09
    • 1970-01-01
    • 1970-01-01
    • 2011-06-14
    • 2012-11-15
    • 1970-01-01
    相关资源
    最近更新 更多