How to get total number of posts in range

I want to be able to display the total number of posts in a range on each element with a $postCount variable like so (it’s obvs 5 in this case but yeh):

{{ range $index, $element := first 5 (where .Data.Pages “Type” “work”) }}
{{ $index }}/{{ $postCount }}
{{ end }}

It seems I need to use the len function I’m just not quite sure what to do with it.

Thanks.

Not sure if this will help. I have this in a shortcode and I show it on my front page:

{{/* Show a count of pages by different types */}}
<p>
    There are {{ len .Site.RegularPages }} pages currently on this site.
    {{ len (where .Site.RegularPages "Section" "=" "posts") }} <a href="/posts">blog posts</a>,
    {{ len (where .Site.RegularPages "Section" "=" "kb") }} <a href="/kb">knowledgebase pages</a>,
    {{ len (where .Site.RegularPages "Section" "=" "about") }} <a href="/about"><em>about</em> pages</a>
    and {{ len (where .Site.RegularPages "Section" "=" "projects") }} <a href="/projects">project pages</a>.
</p>
1 Like

I rewrote it using the examples in this thread in the end and without the “first 5” as it was serving no purpose: Range length or last element?

Thanks anyway :slight_smile:

{{ $pages := first 5 (where .Data.Pages “Type” “work”) }}
{{ $postCount := len $pages }}
{{ range $index, $element := $pages }}
{{ add $index 1 }}/{{ $postCount }}
{{ end }}
3 Likes