How to add number of articles in the main menu


I have a main menu as follows:

{{ $currentNode := . }}
    {{ range .Site.Menus.main }}
    {{- $isCurrent := ( or ( $.IsMenuCurrent "main" . ) ( $.HasMenuCurrent "main" . ) ) -}}
      <a {{ if $isCurrent -}} class='current' aria-current='page' {{ end -}} href='{{ .URL }}'>
        {{- .Name -}}
  {{ end }}

This menu works well but I would like to add for each menu item the number of articles such as books (12).
Sorry but no obvious method either in the Hugo docs or in this forum.
Thanks for help

It’s documented here:

You can count the number of posts for a certain topic, store the number in a variable and publish the variable content with len.

Hope this helps.

If you search in this forum like you’ll of course get answers. My search term was “count number of posts”.

Thanks a lot Leo and sorry.
I saw the two posts you got but my very limited skills in coding don’t allow me to implement it in the above menu.
I don’t know how to use the following example

<a {{ if $isCurrent -}} class='current' aria-current='page' {{ end -}} href='{{ .URL }}'> {{- .Name -}} </a>{ len (where .Site.Pages "Section" "!=" "") }}
or other examples proposed in the forum.

Actually, I succeeded to display what I wanted with the following code:

  <div class="card">
        {{ range $key, $value := .Data.Terms }}
          <a href="{{ (print $key | urlize) | relURL }}">
            {{ $key }}
          ({{ len $value }})
        {{ end }}

But it is displayed only on the single page “site/categories” and not on any other page on the site.
It should be dealt with the terms.html in default layout but I don’t understand the rationale behind this situation.
Help would be much appreciated