Get post counts icategory

Hi,
I’m trying to output the number of posts in a category as described in the Collection documentation.
I am using the blueprints from the demo theme.
In Blog\Post added

categories:
    type: entries
    source: Blog\Category
in Blog\Category added
posts:
    type: entries
    source: Blog\Post
    inverse: categories
    hidden: true

made Save&Migrate

in patrial right_block.htm
3 collections added to patrial right_block.htm

  • blog - Blog\Post
  • blogCategories - Blog\Category
  • blogConfig - Blog

And I’m trying to display the number of posts in each category.

{% set cat = blogCategories.withCount('posts').get() %}
{% set cat = blogCategories.withCount('posts'.get(}}
{% for category in blogCategories %}
    
    <li class="{{ activeBlogCategory == category.slug ? 'active' }}">
        <a href="{{ 'blog/category'|page({ slug: category.slug, id: category.id }) }}">
            {{ category.title }} {{ cat }} 
        </a>
    </li><br/>
{% endfor %}

Returns a list of categories, but it doesn’t contain the number of posts.
What am I doing wrong?

First, {% set cat = blogCategories.withCount(‘posts’.get(}} doesn’t look right.

It seems like it creates an attribute. I suggest you dd or log the variable to see what is the attribute created in your case.

when you have that “inverted” relation, then you can use {{ category.posts.count() }} to show number of posts in specific category :wink:

remove that {% set... %} part of counts…