Hello community!
I really don’t know why this code does not work, so need some clue on where could be problem? It does not show an error, but literally does nothing, but if I change where operator from “=” to “!=” it lists all articles.
Long story short, here is the code:
{{ $productTitle := .Params.title }} /* Current page title i.e: New Item Description 1*/
{{ range where (where .Site.RegularPages "Section" "blog" ) ".Params.relProd" "=" $productTitle }}
<article class="article">
{{ .Params.title }}
</article>
{{ end }}
Here is example of article.md file:
---
title: Lorem ipsum dolor sit amet, consectetur
date: 2021-04-09T13:17:25.161Z
featuredUrl: /uploads/featuredImage.jpg
relProd: New Item Description 1
---
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Here is part of config.yml file:
- name: "blog"
label: "Blog"
folder: "content/blog"
create: true
fields:
- {label: "Title", name: "title", widget: string }
- {label: "Publish Date", name: "date", widget: datetime }
- {label: "Featured Image", name: "featuredUrl", widget: image }
- {label: "Related Product", name: "relProd", widget: relation,
collection: "products", search_fields: ["title"],
value_field: "title", required: false }
- {label: "Body", name: "body", widget: markdown }
Before I actually try to help you, let me warn you that this is not a Netlify-related question and thus, any help that you might get from here might not be accurate. You’d probably be better off by asking this on the framework’s support.
With that being said, I think you’re using Hugo and trying to display related posts (products, in your case). Hugo already has a configuration for that. Have you tried that? Related Content | Hugo? If that’s not helpful, could you share the repo where it’s not working? It would help us reach to conclusions faster.
1 Like
Thanks for quick response and sorry for a bit offtopic for this forum, but still if you could give any advice It would be helpful. Here is a link to git repo
The file with this code is /themes/aurora/layouts/products/single.html
I’ll try this repo in a few mins. Hopefully we can get to a solution.
1 Like
Okay so, I’ve not been able to figure out why exactly that code is not working even though it looks like it should, but I found a workaround. Here’s my snippet:
{{- $pageTitle := .Title -}}
{{- range where .Site.RegularPages "Section" "blog" -}}
{{- if eq .Params.relProd $pageTitle -}}
<article class="article">
{{- .Title -}}
</article>
{{- end -}}
{{- end -}}
This works when the title of the page in products section matches with .Params.relProd
of a page in blog section.
Here’s a snip:
Side note: I had to manually set the title of the new-item-description-1.md
in the products section to adsf
to match the configuration.
1 Like
Thanks for help! This workaround works fine for me as I wont have too many posts, but still it’s weird that example described in Hugo docs does not work.
It does work, I have it working in one of my websites, so I find it strange too that it doesn’t work in your case.
If you want to reach to a conclusion, I’d advise you to ask it on Hugo forums why it’s not working.
On a side note, I think your content needs a lot of cleanup as it was a little difficult to figure out what’s happening because of what especially because of not-properly named files, etc. Maybe if you clean it and organise it better, everything will fall into place.
1 Like
Thanks for advice! I’m new to HUGO and all that Jamstack way of building websites, so may be I just use HUGO not as it is meant to be used)))
UPD
In case someone will face up with the same problem in Hugo here is the answer from Hugo community forum, this solves an issue, but still…WHY? Why eventually Hugo converts case internally)))
themes/aurora/layouts/products/single.html
Change:
{{ range where (where .Site.RegularPages "Section" "blog" ) "Params.relProd" "=" $productTitle }}
To:
{{ range where (where .Site.RegularPages "Section" "blog" ) "Params.relprod" "=" $productTitle }}
.Params
keys are converted to lower case internally. Most of the time you can reference your parameters without regard to case, but it will bite you with where
, sort
, and isset
.
As a best practice, use snake_case when naming your site and page parameters.
Also, in your code, you can use .Title
instead of .Params.title
.