L
L
lavezzi12016-01-06 09:55:52
Twig
lavezzi1, 2016-01-06 09:55:52

How to count how many posts in a category?

Hello. I am making an Archive page, I need to display blocks: Category name (number of articles inside) and a list of articles in it.
Here is the code. How to display the number of articles in the category next to the category name?

{% for category in site.categories %}
  <div id="{{ category | first | remove:' ' }}">
    <h2>{{ category | first }} ({{ category | first | size }})</h2>
    <ul >
      {% for posts in category %}
        {% for post in posts %}
          {% if post.url %}
            <li>
              <a href="{{ post.url }}">{{ post.title }}</a>
              <p><time>{{ post.date | date: "%-d %B %Y" }}</time></p>
            </li>
          {% endif %}
        {% endfor %}
      {% endfor %}
    </ul>
  </div>
{% endfor %}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AngelZeruel, 2016-01-06
@AngelZeruel

category.posts|length

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question