Range over posts in nested section in hugo

803 Views Asked by At

I use this code to show related posts in the same sections for posts in /content/posts. However, the code stops working when I move the content to a nested section, such as content/posts/news, resulting in content from other sections appearing. Could anyone provide a solution or guide me on how to make this work?

{{ range where (where site.RegularPages "Section" .Section) "Permalink" "ne" .Permalink }}
  <a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a>
{{ end }}
1

There are 1 best solutions below

1
On

Try using .CurrentSection.Pages, which is better fit for this than filtering all the results from site.RegularPages.

Then the current page can also be filtered out with a simple where statement.

{{ range (where .CurrentSection.Pages "Permalink" "ne" .Permalink) }}
    <a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a>
{{ end }}