Incorrect grouping by month

Hello.

I have the following code for grouping page:

{{ range sort ( (where (where .Site.Pages "Section" "posts") ".Title" "!=" "Posts" ).GroupByDate "01" ) "Key" "asc" }}

I want to get something like calendar where pages must ordered by month.
But I’ve got the following page:


With month ordered by year too.
Please help how correct realize ordering only by month.

Does really nobody knows?

Nobody knows without your datas…

Hello.
Which data? I just have posts. And needs group them by only month.
Which additional information you need?
Thanks in advance.

For example a list of all the publication dates of your articles. So it would be possible to know where the confusion is.

An example: are the “01” the months of January of different years? Are these years in the right order? Etc.

You are more likely to receive a prompt and accurate response if you post a link to the public repository for your project.

See Requesting Help.

Let us see your code

Include a link to the source code repository of your project, because we really need the context of seeing your templates and partials to be able to help you. It is trivial to do a quick git clone on your repo, then run hugo server in your project, to help you out. On the other hand, recreating your code from screenshots, or sort of guessing at it, is not.

If you can’t share your repository for whatever reason, consider creating a dummy repo that you can share, which reproduces the problem you’re experiencing.

01 is a month.

Now it looks like:

January (2022)
dates (from 2022)

January (2021)
dates (from 2021)

January (2020)
dates (from 2020)

February (2022)
dates (from 2022)

February (2021)
dates (from 2021)

etc…

I need:
January
dates (from all years)

February
dates (from all years)

Sorry but I have not public repository for this project. :roll_eyes:

1 Like

Full single.html for this page:

{{ partial "header.html" . }}
<section id="main">
    <h2>{{ .Title }}</h2>
    {{ range sort ( (where (where .Site.Pages "Section" "posts") ".Title" "!=" "Posts" ).GroupByDate "01" ) "Key" "asc" }}
        <h3>{{ .Key }}</h3>
        {{ $pages := .Pages.ByDate}}
        {{ range $pi ,$p := $pages }}
            <a style="font-size: large;" href="{{ .Permalink }}">{{ $p.Date.Format "02" }}</a>
            {{- if lt $pi (sub (len $pages ) 1) -}}
                <span style="font-size: large;"> </span>
            {{- end -}}
        {{- end -}}
    {{- end -}}
</section>
{{ partial "footer.html" . }}
{{/* Build slice of pages */}}
{{ $p := slice }}
{{ range where site.RegularPages "Type" "posts" }}
  {{ $p = $p | append (dict "month" (.Date.Month) "page" .) }}
{{ end }}

{{/* Range through months */}}
{{ range $key := seq 12 }}
  {{ with where $p "month" . }}
    <h2>{{ printf "%02d" $key }}</h2>
    {{ range . }}
      <a href="{{ .page.RelPermalink }}">{{ .page.LinkTitle }}</a><br>
    {{ end }}
  {{ end }}
{{ end }}

Thank you! Looks similar what I want. Just days not sorted.
Am I correct understand how this works:

  1. We created slice with maps with two keys: “month” - contains month’s number, “page” = contains Page type.
  2. By sequence 1 to 12 we get pages with this sequence index and print link to it.

But I don’t understand why if I change this code

{{ range . }}
      <a href="{{ .page.RelPermalink }}">{{ .page.LinkTitle }}</a><br>
{{ end }}

to this

{{ range sort .page.Date.Day }}
    <a href="{{ .page.RelPermalink }}">{{ .page.LinkTitle }}</a><br>
{{ end }}

I’ve got an error:

 failed to render pages: render of "page" failed: "C:\home\projects\hugo\liucalendar\layouts\calendar\single.html:17:23": execute of template failed at <.page.Date.Day>: can’t evaluate field page in type []map[string]interface {} 

C:\home\projects\hugo\liucalendar\layouts\calendar\single.html:17:23:

    <h3>{{ printf "%02d" $key }}</h2>
    {{ range sort .page.Date.Day }}
      <a href="{{ .page.RelPermalink }}">{{ .page.LinkTitle }}</a><br>
    {{ end }}

Yes.

Do this instead:

{{ range sort . "page.Date" "asc" }}

Doesn’t work. :frowning: Page not sorted still.

Yes, it does. Try it:

git clone --single-branch -b hugo-forum-topic-39118 https://github.com/jmooring/hugo-testing hugo-forum-topic-39118
cd hugo-forum-topic-39118
hugo server

Relevant code in layouts/_default/home.html.

Hm. Ok. thank you for your example.

Finally I did it the following way:

{{- partial "header.html" . -}}

{{/* Build slice of pages  */}}
{{- $data := newScratch -}}
{{- range where site.RegularPages "Type" "posts" -}}
  {{- $monthKey := .Date.Format "01" -}}
  {{- $pp := $data.Get $monthKey -}}
  {{- $pp = $pp | append . -}}
  {{- $data.Set $monthKey $pp -}}
{{- end -}}

<section id="main">
    <h2>{{- .Title -}}</h2>
    {{- range $key := seq 12 -}}
      {{- $monthIdx := printf "%02d" $key -}}
        <h3>{{- $monthIdx -}}</h3>
        {{- $pages := $data.Get $monthIdx -}}
          {{- range $pi ,$p:= sort $pages "Date.Day" -}}
            <a style="font-size: large;" href="{{ .Permalink }}">{{ $p.Date.Format "02" }}</a>
          {{- if lt $pi (sub (len $pages ) 1) -}}
            <span style="font-size: large;"> </span>
          {{- end -}}
        {{- end -}}
    {{- end -}}
</section>
{{- partial "footer.html" . -}}

@jmooring thanks for your idea!

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