如果类别中没有Jekyll帖子,如何创建IF ELSE语句?

ast*_*ush 23 jekyll

我有一个FOR语句,输出所有类型的帖子jobs.

{% for post in site.categories.jobs %}
  <article>
    <h3><a href="{{ post.permalink }}">{{ post.title }}</a></h3>
    <p>{{ post.summary }}</p>
  </article>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

但如果没有已发布的帖子,jobs我想显示"我们现在不招聘"的消息.

你能创建一个IF/ELSE语句来检查特定类别的帖子吗?

Yi *_*eng 36

尝试检查一下{% if site.categories.jobs == null %}.

{% if site.categories.jobs == null %}
  <p>We're not hiring right now</p>
{% else %}
  {% for post in site.categories.jobs %}
    <article>
      <h3><a href="{{ post.permalink }}">{{ post.title }}</a></h3>
      <p>{{ post.summary }}</p>
    </article>
  {% endfor %}
{% endif %}
Run Code Online (Sandbox Code Playgroud)

  • Jekyll也使用[Liquid Template Language](https://github.com/Shopify/liquid/wiki/Liquid-for-Designers)进行参考. (6认同)