Skip to content
● Meet Grav Guard: keep every Grav site you run secure and up to date, all from one place. Free for up to 3 sites. Read more →

Number of posts in Category

Solved by Karmalakas View solution

Started by Pedro M 4 years ago · 4 replies · 621 views
4 years ago

Hi.

How can I know the number of posts on the blog page, filtered by category?

I have looked in the forum, an entry that explains how to get the categories of the blog. Now I want to know how many posts are in each category.

Example:
4 posts in category one, 5 posts in category two, etc.

Thanks so much, in advance.

4 years ago

Based on the answer you've linked, here's a quick solution, which I think should work, but not tested

TWIG
{% set items = page.evaluate({'@page.children': '/items'}) %}
{% set catItemsCounts = [] %}

{% for item in items %}
    {% for cat in item.taxonomy.category %}
        {% set catItemsCounts[cat] = ( catItemsCounts[cat] | default(0) ) + 1 %}
    {% endfor %}
{% endfor %}
👍 1
4 years ago

Hi @Karmalakas

The line {% set catItemsCounts[cat] = ( catItemsCounts[cat] | default(0) ) + 1 %} shows the following error:

TXT
Unexpected token "punctuation" of value "[" ("end of statement block" expected).
4 years ago

Thanks @Karmalakas.

Your solution works fine as I expected.

Suggested topics

Topic Participants Replies Views Activity
General · by themetman, 1 day ago
2 41 18 hours ago
General · by Hanns Mattes, 1 week ago
0 122 1 week ago
General · by Karmalakas, 1 week ago
2 185 1 week ago
General · by Hanns Mattes, 3 weeks ago
0 235 3 weeks ago
General · by Dima, 4 weeks ago
1 211 4 weeks ago