Paginate all subsection posts from a particular section

I’m trying to paginate for a layout template page all pages that fall in subsections.

The basic layout of my sections is like:

/qa/volvo/
/qa/volvo/modelA/
/qa/volvo/modelB/
/qa/bmw/
/qa/bmw/modelX/
/qa/bmw/modelZ/

Now for the layout of the /qa/volvo/ page, I want to show all content from its subsections (/qa/volvo/modelA/ and /qa/volvo/modelB/). Of course, the .IsDescendant method (from the page variables) seems perfectly capable for that task.

Except that I cannot come up with a where filter that works with .IsDescendant. For instance:

{{ $thisPage := .CurrentSection }}
{{ $articles := (where .Site.RegularPages ".IsDescendant" $thisPage) }}

error calling where: IsDescendant is a method of type *hugolib.Page but doesn’t satisfy requirements

Some of the other angles I considered:

  • The .Section page variable doesn’t help here, since it only returns the top most section ("qa" in my case).
  • .Paginator.Pages only paginates the pages in the section itself, not those of its subsections.
  • I couldn’t find another page variable that I could use to infer which subsection a post falls into.

Who knows how I can paginate all posts from the subsections? :slight_smile:

(My apologies if it’s basic question or I overlooked something simple; I’ve tried many approaches but now don’t know what ‘up’ and ‘down’ is anymore. :smile:)

I don’t have an answer but, I wonder if .CurrentSection actually works as a page, that is expected as the argument for .IsDescendant.

An interesting test page:

https://maku77.github.io/hugo/list/sidebar-menu-demo.html

1 Like

Starting point, maybe:

From what I understand it does. This code, for instance, gives me the proper output:

{{ $thisPage := .CurrentSection }}

{{ range .Site.RegularPages }}
    {{ .RelPermalink }} - {{ .IsDescendant $thisPage }} <br>
{{ end }}

But that’s not something I can paginate.

Thanks for that interesting link. It does suggest that .IsDescendant is one valid way to check whether a page belongs to a section higher up the hierarchy or not.

Hopefully there’s also a workaround that gives something we can paginate. :slight_smile:

I think the answer might be in .Sections, because that page variable returns the sections that fall below the current content.

For the /qa/volvo/ page, that would be the /qa/volvo/modelA/ and /qa/volvo/modelB/ sections. (And that’s indeed how it works in my theme.)

And so to see if any page of the website (.Site.RegularPages) falls in a subsection of the current page, I’d code that like so:

{{ range where .Site.RegularPages ".CurrentSection" "in" .Sections }}
    {{ .RelPermalink }}<br>
{{ end }}

Unfortunately this doesn’t yield an outcome, which surprises me a little bit. Both .CurrentSection and .Sections yield a page, although the first returns one page and the latter can return multiple. But either way in compares are page with a page here.


Edit: I’m coming very close with the following code:

{{ $articles := where .Site.RegularPages "Section" "qa" }}
{{ $paginator := .Paginate (where $articles ".Parent.Parent.Title" .CurrentSection.Title) }}

This performs the filtering exactly like I want it. But unfortunately, this triggers the “error calling Paginate: invoked multiple times with different arguments” error message, even though I do use the .CurrentSection.Title variable to generate a different where filter on each section page.