Previnsection / nextinsection not ordering ByTitle ordering

But, on my pages, when i go previous/next, it doesn’t do it by title. I have a file 01.001.md and a file 01.002.md; it should have the next be file 01.002.md, but instead it skips ahead, using the date of the files instead

My layouts/_default/list.html is a recursive list:

{{ define "main" }}

  <div class="w-full max-w-screen-xl lg:px-4 xl:px-8 mx-auto">
    <div class="grid grid-cols-2 lg:grid-cols-8 gap-4 lg:pt-12">
      <div class="col-span-2 lg:col-start-2 lg:col-span-6 bg-secondary-bg rounded px-6 py-8">
        <div class="content">
              <ul>
                {{ range .Pages.ByTitle }}
                  {{ template "walk" . }}
                {{ end }}
              </ul>
  </div></div></div></div></div>
{{ end }}

{{- define "walk" }}
  <li><a href="{{ .RelPermalink }}">{{ .Title }}</a></li>
    {{- with .Pages.ByTitle }} 
      <ul>
        {{- range . -}}
          {{- template "walk" . -}}
        {{- end }}
      </ul>
    {{- end }}
  </li>
{{- end -}}

Post footer code in /layouts/_default/partials/components/post-footer.html is as follows

{{ if or .PrevInSection .NextInSection }}
<div class="flex flex-col md:flex-row md:justify-between -mx-2 mt-4 px-2 pt-4 border-t">
    <div class="md:text-right mt-4 md:mt-0">
        {{ with .PrevInSection }}
        <span class="block font-bold">{{ i18n "previous" }}</span>
        <a href="{{ .Permalink }}" class="block">{{ .LinkTitle }}</a>
        {{ end }}
    </div>
    <div>
        {{ with .NextInSection }}
        <span class="block font-bold">{{ i18n "next" }}</span>
        <a href="{{ .Permalink }}" class="block">{{ .LinkTitle }}</a>
        {{ end }}
    </div>
</div>
{{ end }}

Here’s how I got mine to work by sorting by weight. Perhaps you can try the same since I found sorting by weight more reliable (that’s if you don’t want to use dates).

@tknx

The page methods Next, NextInSection, Prev, and PrevInSection return a page based on Hugo’s default sort order, not the sort order of the current range loop or section.

Although it is well documented, the behavior is a frequent discussion topic in this forum.

This example shows three different range loops on the same list page:

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

And the results are not what most of us would expect/want.

I figured this out by using the following in my footer:

{{ if or .PrevInSection .NextInSection }}
{{ $pages := where site.RegularPages “Section” .Section }}

{{ with $pages.ByTitle.Next . }} {{ i18n "previous" }} {{ .LinkTitle }} {{ end }}
{{ with $pages.ByTitle.Prev . }} {{ i18n "next" }} {{ .LinkTitle }} {{ end }}
{{ end }}

Not sure what you mean here.

Next, NextInSection, Prev, and PrevInSection are all static, so there can only be one Next, and I fail to see why ByTitle would make a better choice.

Which is also why you can do

{{ with site.RegularPages.ByTitle.Next . }}{{ end }}

Etc.

I use by title for my specific use case but I’ll try your code. Seems more efficient.

I am not suggesting that it would be.

Conceptually, I think the static nature of these methods is challenging for new users to comprehend. For example…

If I’m visiting /posts/post-1, and the /posts page collection is sorted .ByParam "foo", then Next, NextInSection, Prev, and PrevInSection will probably return something other than the next or previous page in the collection (unless the sort order happens to match the default sort order).

The Next and Prev methods on Pages provide the desired results, but one must define the page collection in both the list and single templates, and remember to keep them in sync.

That may be so, but I would find myself in a confusing Twilight zone if somehow …

{{ (site.GetPage "my-first-article").Next.Title }}

…printed something different depending on where I put that block.

1 Like

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