Iterating over posts produces unexpected results

I’ve got a template that looks like this:

<div class="site-content">
    <div class="inner">
        <main class="site-main">
            <article class="post">
                <header class="entry-header">
                    <div class="entry-header-wrap">
                        <h1 class="entry-title">{{ .Title }}</h1>
                    </div><!-- .entry-header-wrap -->
                </header><!-- .entry-header -->
                <div class="entry-content">
                    {{ range (where .Site.Pages "Type" "post").GroupByDate "2006" }}
                    <h2 class="archive-title">{{ .Key }}</h2>
                    {{ range .Pages }}
                    <article class="archive-item">
                        <a href="{{ .Permalink }}" class="archive-item-link">{{ .Title }}</a>
                        <span class="archive-item-date">
                                    {{ .Date.Format "January 2, 2006" }}
                                  </span>
                    </article>
                    {{ end }}
                    {{ end }}
                </div><!-- .entry-content -->
            </article><!-- .post -->
        </main><!-- .site-main -->
        {{ partial "sidebar.html" . }}
    </div><!-- .inner -->
</div><!-- .site-content -->

I think the code is right, but it’s not doing what I want it to and I’m not really sure how to debug it. At the top of the list, there’s a “Posts” link that points back to the Post listing. You can see it happening here: https://cweagans.net/archive/

Am I doing something wrong here? Or is the range directive doing something wonky? I’d like to narrow down if this is a Hugo bug or not.

Try…

{{ range (where .Site.RegularPages "Type" "post").GroupByDate "2006" }}

For added reference, this is because .Pages for .Site.Pages will also include index pages for a section. I’m guessing you have a file at content/posts/_index.md, perhaps with title = posts in the front matter?

I’d have to see more source; otherwise, I’m just taking stabs in the dark…

That fixed it. Thanks!!

1 Like

There is .Site.RegularPages which is probably what you want.