Series article x of y not working for list

Hi,
I am creating a series section for my own site (no theme) and I have the taxonomy working.

  • I can get a list of series that exist with number of articles in each
  • For the single page I am able to get “this is article X of Y in the FOO series”
  • For the list of articles in FOO series I can get the the series that the article belongs too. But I am not able to get this is article X of Y in the FOO series"

A point in the right direction would be great as the resources I have found on the web just fall short for me being able to resolve this myself.

The relevent snippet in the /layout/_default/single (I have the same in my /layout/_default/list.html) is:

 {{- with .Params.series }}
            <div>
              <i class="fa-solid fa-layer-group"></i>
              Series:
              {{- range $seriesName := . -}}
                {{ with $.Site.GetPage (printf "/%s/%s" "series" $seriesName ) }}
                  <a class="tag" href="{{ .RelPermalink }}">{{ .Title }}</a>
               {{- end }}
            
              {{ $currentSeries := index $.Site.Taxonomies.series ($seriesName | urlize) }}
              {{ if $currentSeries }}
                {{ $total := len $currentSeries.Pages }}
                {{ range $index, $page := $currentSeries.Pages }}
                  {{ if eq $page.Permalink $.Permalink }}
                    {{ $articleNumber := add $index 1 }}
                        (Article {{ $articleNumber }} of {{ $total }})
                    {{ end }}
                  {{ end }}
                {{ end }}
              {{- end }}
            </div>
          {{- end }}
        </div>
        {{- end }}

A live version of my site is at
https://mikewebbtech.github.io/mikewebb-tech/series/

and the site repo on GitHub is at
https://github.com/mikewebbtech/mikewebbtech-hugo

Cheers
Mike

OK! I think I worked it out the issue. Context, all about the context. still getting my head around that concept.

this was never going to work in a list page the same as it works in a single page

{ if eq $page.Permalink $.Permalink }}

So, I think what is happening is that inside my nested range for the series pages, . shifts and is referring to the series page I am looping over. Resulting in {{ .RelPermalink }} inside the inner loop not pointing to the current article anymore…I think

Now to try and reslove that

Yep that was the issue. while trying to solve this. I got explicite and assigned “.” to
{{ $article := . }} so I can reference it no matter how nested I used it, like

{{ if eq $page.RelPermalink $article.RelPermalink }}

comparing the the relative permalink of the current article in the loop.

and then, to be sure to be sure:

 {{ with $.Site.GetPage (printf "/series/%s" $seriesName) }}

So this worked for me. Not sure if there is a better way or if I just put a bandaid over a bandaid covering spahgetti code.

{{ $article := . }}
          {{ with $article.Params.series }}
            <div>
              <i class="fa-solid fa-layer-group"></i>
              Series:
              {{ range $seriesName := . }}
                {{ with $.Site.GetPage (printf "/series/%s" $seriesName) }}
                  <a class="tag" href="{{ .RelPermalink }}">{{ .Title }}</a>
                {{ end }}
          
                {{ $currentSeries := index $.Site.Taxonomies.series ($seriesName | urlize) }}
                {{ if $currentSeries }}
                  {{ $total := len $currentSeries.Pages }}
                  {{ $articleNumber := 0 }}
                  {{ range $index, $page := $currentSeries.Pages }}
                    {{ if eq $page.RelPermalink $article.RelPermalink }}
                      {{ $articleNumber = add $index 1 }}
                    {{ end }}
                  {{ end }}
                    (Article {{ $articleNumber }} of {{ $total }})
                {{ end }}
              {{ end }}
          </div>
          {{ end }}

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