简体   繁体   中英

How do I create an IF ELSE statement if there are no Jekyll posts in a category?

I have a FOR statement that outputs all posts of type jobs .

{% for post in site.categories.jobs %}
  <article>
    <h3><a href="{{ post.permalink }}">{{ post.title }}</a></h3>
    <p>{{ post.summary }}</p>
  </article>
{% endfor %}

But if there are no published posts in jobs I would like to display a "We're not hiring right now" message.

Can you create an IF/ELSE statement to check for posts in a specific category?

Try check it with {% 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 %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM