Combine Related Posts with specified posts

I’m trying to do a simple take on this that prioritizes explicitly specified related posts and fills the rest of the spots with the generated Related posts but also finding it’s taking me a frustratingly long time to get it to work.

It’s clear that mapping these URLs to the Page object won’t work because the apply doesn’t work with GetPage (would appreciate if this were explicitly stated in the documentation). I’m now trying to do it the other way where I map the Pages returned from .Site.RegularPages.Related to their .RelPermalink but can’t seem to find a way to do this. Am I missing something?

Hi there,

I’ve moved your question to a new topic because the one you were replying to is old, and has been marked solved.

Please have have a read about Requesting Help to see how to ask for help.

With this code I get a couple of specific pages (contact, about) at the top of the list, followed by the first three related pages (or none if there aren’t any related pages).

{{- $specificPages := slice -}}
{{- $specificPlusRelatedPages := slice -}}

{{- with .Site.GetPage "/page/contact.md" -}}
  {{- $specificPages = $specificPages | append . -}}
{{- end -}}
{{- with .Site.GetPage "/page/about.md" -}}
  {{- $specificPages = $specificPages | append . -}}
{{- end -}}

{{- $relatedPages := .Site.RegularPages.Related . | first 3 -}}

{{- with $relatedPages }}
  {{- $specificPlusRelatedPages = $specificPages | append . -}}
{{- end -}}

{{- with $specificPlusRelatedPages }}
<ul>
  {{- range . }}
  <li><a href="{{ .RelPermalink }}">{{ .Title }}</a></li>
  {{- end }}
</ul>
{{ end -}}

Ah thank you @jmooring. I didn’t know of the append function. Got what I wanted with this piece of code.

{{ $pageurls := apply (.Params.related | default slice) "string" "." }}
{{ $linkedPages := slice }}
{{ range $pageurls }}
    {{ $linkedPages = $linkedPages | append (. | $.Site.GetPage) }}
{{ end }}
{{ $related := $linkedPages | union (.Site.RegularPages.Related .) | first 5 }}

Appreciate the help!

(although I guess my intended functionality relies on union preserving order but I think append will allow me to deal with that when the time comes).

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