user8497193
user8497193

Reputation:

How to check amount of posts?

I have this code:

---
layout: default
---
<hr>

{% assign posts_amount = site.posts.length %}
{% if posts_amount == 0 %}
  <h2>Sorry :(</h2>
  <p>At the moment, content isn't available for you. Check me later!</p>
  <hr>
{% else %}
  {% for post in site.posts %}
    <h2>
      <a href="{{ post.url | relative_url }}">{{ post.title | escape }}</a>
    </h2>
    <p>{{ post.date | date: "%Y-%m-%d" }}</p>
    <hr>
  {% endfor %}
{% endif %}

<p>Subscribe <a href="{{ "/feed.xml" | relative_url }}">via RSS</a>.</p>

but it's not working, I don't know why.

Jekyll: newest version (3.6.2), with jekyll-feed (0.9.2), and jekyll-seo-tag (2.3.0).

Any ideas?

Upvotes: 1

Views: 65

Answers (1)

Neil Anderson
Neil Anderson

Reputation: 1375

You can use dot notation and you don't need the assign

---
layout: default
---
<hr>

{% if site.posts.size == 0 %}
  <h2>Sorry :(</h2>
  <p>At the moment, content isn't available for you. Check me later!</p>
  <hr>
{% else %}
  {% for post in site.posts %}
    <h2>
      <a href="{{ post.url | relative_url }}">{{ post.title | escape }}</a>
    </h2>
    <p>{{ post.date | date: "%Y-%m-%d" }}</p>
    <hr>
  {% endfor %}
{% endif %}

<p>Subscribe <a href="{{ "/feed.xml" | relative_url }}">via RSS</a>.</p>

Upvotes: 2

Related Questions