Featured Post help

Hello, I’m working on a blog and trying to create a Featured Post thing. I initially found a thread here, but the thread is a few years old and didn’t quite give me the answer I need. (Best practice to set post as featured)

I’m still very new so I don’t know if I’m calling things their proper name, but I have a file in content called _index.md which has front matter featured_post = "posts/test-post-original.md" and this is controlled by a dropdown in Forestry which selects a Post. All of that functionality is good except that when I try to access .Params.featured_post all it contains is a string. In Javascript (my main language) this might be a whole post object and I could access all of its .Params. How can I get this effect in Hugo? Or, what is the correct way to do this? Thank you very much for your help

I’m now thinking maybe using some way to iterate through all of the posts like how I use this elsewhere

{{ range (where .Site.RegularPages "Section" "posts") }}
  {{ .Render "post-tile" }}
{{ end }}

but maybe there’s a way to only show the post where the file name matches the value of featured_post? Not sure how to write this.

Use site.GetPage: https://gohugo.io/functions/getpage/

{{ if isset .Params "featured_post" }}
  {{ with .Site.GetPage .Params.featured_post }}{{ .Title }}{{ end }}
{{ end }}
1 Like

Perfect, this works exactly as needed!

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