My goal is to iterate over related_posts. Then for each link, I want to display a HTML link with image from it’s frontmatter.
Expected output
With reference to the above code, the output should be something like this:
<a href="https://www.example.com/posts/nice-post/"> <img src="/*image from the frontmatter of nice-post.md*/"/></a>
<a href="https://www.example.com/posts/nice-post2/"> <img src="/*image from the frontmatter of nice-post2.md*/"/></a>
<a href="https://www.example.com/life/nice-post3/"> <img src="/*image from the frontmatter of nice-post3.md*/"/></a>
How do I achieve this ?
What I know so far
{{ with .Params.related_posts }}
{{ range . }}
{{ $url := (index . "link") | safeURL }}
<a href="{{$url}}> </a>
{{ end }}
{{ end }}
I also know that I have to use GetPage.
PS: Is there any better way of accessing front matter of another page instead of using this method ?
It would be much simpler to use Hugo’s Related Content feature to display the link and the thumbnail of related posts on a post’s page, rather than trying to do, what you describe above, in the amended first post.
I appreciate @alexandros for your suggestion. I am aware of related content in Hugo. The thing is that, I want fine grain control over what gets displayed on each page. That’s why I am storing related posts in frontmatter.
Thanks a ton @jmooring for that suggestion. I just had another idea of doing this using urls.Parse | Hugo (gohugo.io) function. But in doing so, once again I am coming back to perform a reverse lookup from URL to .File.Path & the url path need not be equal to file path always.