The code below displays related posts in ‘person/single.html’ if ‘.Params.name’ in a ‘person’ content type matches ‘.Params.person’ in a ‘post’ content type. What I’m trying to do now is to add a “No related posts” message if there are no related posts for a person. I tried: {{ if isset .Params "person }} before the range, but it didn’t work. Her’s a snippet of my code:
content/post/2018-01-18-john-smith.md title: Post title here person: John Smith summary: lorem...
content/person/john-smith.md name: John Smith bio: lorem...
…\themes\mytheme\layouts\person\single.html
// get person name
{{ $person := .Params.name }}
// get posts
{{ $posts := where .Site.Pages "Section" "post" }}
// find all posts where .Params.person (in 'post') matches .Params.name (in 'person')
{{ $relatedPosts := where ($posts) ".Params.person" "=" .Params.name }}
<ul>
{{ range (where $relatedPosts ".Params.person" "ne" nil) }}
{{ partial "components/post-list-item.html" . }}
{{ end }}
</ul>
Ok, thanks for the help @RickCogley & @Mikhail ; it still didn’t work. For some reasons, it looks like the problem is when.Params.person is not set. For the code below: if both .Params.person and .Params.name are set, it prints TEST: Related news found - if .Params.person is not net, it doesn’t print anything.
{{ range $relatedPosts }}
{{ if .Params.person }}
<p>TEST: Related news found</p>
<ul>
{{ partial "components/post-list-item.html" . }}
</ul>
{{ else }}
<p>No related news.</p>
{{ end }}
{{ end }}