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

astanush picture astanush · May 29, 2014 · Viewed 20k times · Source

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?

Answer

Yi Zeng picture Yi Zeng · May 29, 2014

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 %}