Loop through a nested section for the homepage / index.html

Hey everyone, sorry if this was asked before. I’m trying to loop through only the pages in the projects section on my home page. Instead, I’m getting all of the pages including about.html and contact.html.

I’m trying to exclude the last two. Any input would be appreciated.

content
├── projects
│ ├── first.md
│ ├── second.md
│ ├── third.md
├── contact.html
├── about.html

	<ul>
			{{ range .Paginator.Pages.ByPublishDate.Reverse }}
				<li style="padding: 10px;">  
					<a href="{{ .RelPermalink }}">
						<div>
							{{ .Date.Format "2006-01-02" }}
						</div>
						<div>
							{{ .Title }} 	
						</div>
					</a>
				</li>
			{{ end }}
            {{ template "_internal/pagination.html" . }}

Try this (haven’t tested yet since I’m on mobile )

<ul>
{{ $p := where site.RegularPages "Section"  "projects" }}
			{{ range (.Paginate $p).Pages.ByPublishDate.Reverse }}
				<li style="padding: 10px;">  
					<a href="{{ .RelPermalink }}">
						<div>
							{{ .Date.Format "2006-01-02" }}
						</div>
						<div>
							{{ .Title }} 	
						</div>
					</a>
				</li>
			{{ end }}
            {{ template "_internal/pagination.html" . }}

Hey thanks for the quick response.

That method didn’t resolve the issue

Getting the same result.

It works properly when I’m on the projects page, but from my understand it’s because range.Pages loops over the current directory / section. I’m just trying to reproduce that same effect on the home page

include a link to the source code repository of your project

Here ya go:
Link

Doesn’t exist.

probably private?

Sorry, relatively new to this.

Link

yes! that was the case. thanks for pointing that out

Add this code to the front matter of both pages

_build:
  list: never

That solved the issue!

Would you point me out to the docs or expand on how you came to that solution ?

Thanks again!

There’s a simpler way to handle this. Build and sort the page collection before you paginate.

{{ $p := (where site.RegularPages "Type" "projects").ByPublishDate.Reverse }}
{{ range (.Paginate $p).Pages }}
  <h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
{{ end }}
{{ template "_internal/pagination.html" . }}
2 Likes

@luckyduckyquackquack use this instead. But take note of the link I shared in case you want to use it in future.

thanks for this solution!

thanks, will do :saluting_face:

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