HUGO

Filtering by section does not work properly

I am having trouble generating a list of blog posts and projects on my index page.

I have 2 sections: blog and projects.

In /layout/index.html I have:

<h3>blog</h3>
<ul>
    {{- range (.Paginate (where site.RegularPages "Section" "blog" | first 3 )).Pages }}
        <li><a href="{{ .RelPermalink }}">{{ .Title }}</a></li>
    {{- end -}}
</ul>

<h3>projects</h3>
<ul>
    {{- range (.Paginate (where site.RegularPages "Section" "projects" | first 3 )).Pages }}
        <li><a href="{{ .RelPermalink }}">{{ .Title }}</a></li>
    {{- end -}}
</ul>

And this content directory.

content
β”œβ”€β”€ blog
β”‚   └── 1.md
β”œβ”€β”€ contact.md
└── projects
    └── 2.md

1.md looks like this:

---
title: 11
---

111

Same for 2.md, except with 2s instead of 1s.

So, with this configuration, I would expect my generated list to be something like:

blog
- 11

projects
- 22

However, I get this:
maybe_hugo_bug

What am I doing wrong? If this is a bug, what can I do to work around it?

You are invoking .Paginate twice. See:
https://gohugo.io/templates/pagination/#list-paginator-pages

The .Paginator is static and cannot change once created.

Since you are only listing 3+3=6 pages, why paginate at all?

1 Like

Thank you!

You helped me come up with a solution:

{{- range where site.RegularPages "Section" "projects" | first 3 }}

I found the old code on some old thread and thought it was some kind of magic that’s supposed to just work. Anyway, the problem is now solved.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.