Skip to content
Grav 2.0 is officially stable. Read the announcement →
General

Number of posts in Category

Solved by Karmalakas View solution

Started by Pedro M 4 years ago · 4 replies · 392 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 Jerry Hunt, 4 days ago
2 136 1 day ago
General · by pamtbaau, 1 day ago
1 93 1 day ago
General · by Andy Miller, 2 days ago
0 77 2 days ago
General · by Marcel, 12 months ago
6 389 5 days ago
General · by Duc , 6 days ago
3 72 6 days ago