Show list of related pages from a list of filenames in Frontmatter

Hi. In my posts I can add a list of URL’s to related posts. (This is easily done when using the Forestry.io CMS). It will then look like this in Frontmatter:

related:
- posts/article-name-one.md
- posts/article-name-two.md

But I can not figure out how to range through the .Pages, to show only the related ones? I then want these pages to show with .Title

Hope someone would know how to do this. Thx.

Since you are specifying the paths to the related pages manually, you could just range through your related list and use .GetPage.

Something like below (untested) :

{{ with .Params.related }}
  {{ range . }}
    {{ with $.Site.GetPage . }}
      {{ .Title }}
    {{ end }}
  {{ end }}
{{ end }}

Thx. It did not throw any errors, but returned empty.
It ranges fine, but the {{ with $.Site.GetPage . }} is empty.

Thx. I did use this, and works fine with the TAGS, but I want more control of what pages to relate in my posts. And since Forestry.io has this “SORTABLE SELECT FIELD” where we can choose from pages in a section, its very simple and fast.

It also works fine with any front matter param. But up to you.

If had the knowledge of how to use the .Related solution with my current related list of filenames, it would absolutely be the preferred method. I have read https://regisphilibert.com/blog/2018/04/hugo-optmized-relashionships-with-related-content/, but still cant figure out how to use this with my list of filenames.

  1. What version of Hugo are you using?
  2. Do the related pages that you list exist? From the example in the docs page I linked to above: GetPage | Hugo

This method wil return nil when no page could be found, so the above will not print anything if the blog section is not found.

  1. Do you have code we can look at? It makes it easier for us to help you.
  1. I use on this project 0.42.2
  2. Yes
  3. Here are the code:

FMT

related:
- aktuelt/homeclu.md
- aktuelt/elbil-lader.md
- aktuelt/ledig-stilling-elektriker-med-fagbrev.md

SINGLE TEMPLATE

<h4>Relaterte sider</h4>
{{ with .Params.related }}
  {{ range . }}
    {{ with $.Site.GetPage . }}
      {{ .Title }}
    {{ end }}
  {{ end }}
{{ end }}

I updated my HUGO-BIN file to 0.37.0 (Hugo 0.49.2) and your code works fine:grinning:

I just remebered the .Site.GetPage had been updated (simplified). Thanx!