Previous and next 3 posts

I found this post when looking for a similar implementation.

  1. My section has several subfolders and folders inside subfolders which are several levels deep.
  2. The regular pages and _index.md pages are grouped by weight. That is, all regular pages begin with weight of 1 going up (no matter the folder they are in) and all _index.md files have their weight beginning with 2000 going up. (I intend to publish content that will add up to close to 1900 pages in a few years, hence the weight being set like that).
  3. I want to show the previous 3 posts and the next three posts to the current posts based on weight. However, I want this to be based on the first section, not the current section or parent one.

Is that possible? I tried the example shared on the referenced post, but it does not yield the correct result when I replace .CurrentSection with .FirstSection and “Date” with “Weight”. cc @jmooring

Edit: Or can the page methods be modified to achieve the same?

After some testing, page methods are ruled out. It seems impossible without .CurrentSection. But any ideas are welcome.

I’ve read your post several times and I still don’t understand what you want.

Picture your linked answer. Now, instead of getting three previous posts based on current section, I want them to be based on their first section instead, based on weight. All these posts are in different subfolders.

├───book
│   ├───chapter-1
│   │   ├───article-1
│   │   ├───article-2
│   │   └───article-3
│   ├───chapter-2
│   │   ├───part-1
│   │   │   ├───article-1
│   │   │   ├───article-2
│   │   │   ├───article-3
│   │   │   └───article-4
│   │   ├───part-2
│   │   │   ├───article-1
│   │   │   ├───article-2
│   │   │   ├───article-3
│   │   │   ├───article-4
│   │   │   ├───article-5
│   │   │   └───article-6
│   │   ├───part-3
│   │   │   ├───article-7
│   │   │   └───article-8
│   │   └───part-4
│   │       ├───article-9
│   │       ├───article-10
│   │       └───article-11

E.g if I am on chapter 2, I want to get articles 1,2 & 3 of chapter one, when I am on article 1 of chapter 2. All these articles are grouped by weight. So, article-3 of chapter 1 is weight: 3 while article-1 of chapter 2 is weight: 4 and so on. The chapters and parts each has an _index.md file with a different weight, beginning from weight: 2000 in the book sections _index.md.

I think this is what you want…

layouts/_default/single.html
{{/* Get the current page, just to avoid context confusion later on. */}}
{{ $currentPage := . }}

{{/* Get the section pages within the top level section. */}}
{{ $topLevelSectionPages := where .FirstSection.Pages.ByWeight "Kind" "section" }}

{{/* Get the first subsection. *}}
{{/* This assumes your "2000+" weights are correct. */}}
{{ $firstSubsection := index $topLevelSectionPages 0 }}

{{/* Get the regular pages within the first subsection. */}}
{{ $regularPagesInFirstSubsection := $firstSubsection.RegularPages }}

{{/* Display the regular pages in the first subsection. */}}
{{/* We will insert the current page to show where it fits in the weight sorting. */}}
{{/* Remove this later; just showing what you get. */}}
<h2>Regular pages in first subsection</h2>
<p><em>We inserted the current page (highlighted) to show where it fits in the weight sorting.</em></p>
{{ $p := union $regularPagesInFirstSubsection (slice $currentPage) }}
{{ range $p.ByWeight }}
  {{ if eq . $currentPage }}
    <h3 style="background: yellow;"><a href="{{ .RelPermalink }}">{{ .File.Path }}</a> (weight = {{ .Weight }})</h3>
  {{ else  }}
    <h3><a href="{{ .RelPermalink }}">{{ .File.Path }}</a> (weight = {{ .Weight }})</h3>
  {{ end }}
{{ end }}

{{/* Display the previous three pages by weight. */}}
{{/* There might be less than three, or zero, depending on weights. */}}
<h2>Previous three pages (by weight)</h2>
{{ range where $regularPagesInFirstSubsection.ByWeight "Weight" "lt" $currentPage.Weight | last 3 }}
  <h3><a href="{{ .RelPermalink }}">{{ .File.Path }}</a> (weight = {{ .Weight }})</h3>
{{ end }}

{{/* Display next three pages by weight. */}}
{{/* There might be less than three, or zero, depending on weights. */}}
<h2>Next three pages (by weight)</h2>
{{ range where $regularPagesInFirstSubsection.ByWeight "Weight" "gt" $currentPage.Weight | first 3 }}
  <h3><a href="{{ .RelPermalink }}">{{ .File.Path }}</a> (weight = {{ .Weight }})</h3>
{{ end }}

And to make sure we’re using the same terminology:

  • The first section is “book”
  • Under “books” the first subsection is “chapter-1”

This worked, except for one issue. Is it possible to make the part below agnostic? Because after chapter 1, nothing shows up in chapter two articles unless I change 0 to 1 (then the order in chapter one is disrupted since the next article is the first one in chapter 2 rather than the second one in chapter 1). And since some parts may be nested deeper than the structure I shared above, then it means that part may then change again, disrupting other parts.

{{ $firstSubsection := index $topLevelSectionPages 0 }}

If I understand your question correctly, a weight of zero is the same as no weight, so it doesn’t appear as you would expect in the sort order.

You can use negative weights, and positive weights, but not zero.

Forgive me as I am not very technical with Hugo terms. If I understand that code, it calls the first directory e.g. chapter-1. But for part-1, it does not show anything, but instead, every posts in chapter-2 show the three previous posts as those inside chapter-1. The sequence works if I change 0 to 1, but then chapter 1 fails to show anything. I assume this will also happen if there are folders nested more leves deep. So, I am trying to avoid that from happening.

Instead of me trying to guess the specifics of your site, try this:

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

There is a conflict with certain nested folders of my section making this code not to work as expected. Let me go over it again over the weekend.

Please pull the repository again. There was an intermittent problem displaying the current page (highlighted) in the list. I was appending the current page to the list using append instead of union.

The results (next 3 and last 3) were correct; it was just a display issue.

No. That was just an example. I tried to clarify my first post and I might have created some confusion. I want the last three regular pages and the next three regular pages relative to the current post (based on weight). And this without any regard for which folder the page is in.

I thought that was what you wanted.

No. That was just an example. I tried to clarify my first post and I might have created some confusion. I want the last three regular pages and the next three regular pages relative to the current post (based on weight). And this without any regard for which folder the page is in.

Uh, yeah.

English is a third language. Sometimes, finding the right words is a challenge. Sorry about that.

Please pull the repository again. Related code in layouts/_default/single.html.

I modified your code a little bit to get regular pages and in one section only because only one section is required and using weights. But this is brilliant! Thank you. :grin:. I will save it for future use.

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