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


#1

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.


#2

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 }}

#3

#4

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


#5

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.


#6

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


#7

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.


#8
  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: https://gohugo.io/functions/getpage/

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.

#9
  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 }}

#10

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!