How to display an image from a "related" page

Here’s something that seems like it should be simple but I can’t figure out.

I have two two types of content: companies and jobs. Companies are stored in content/en/directory and each have a profile page displaying their logo, description, etc. Jobs are posted by companies and stored in content/en/jobs. They each have a description and reference to the company that created the job posting. The company reference is stored in the job front matter using the key-value pair company: uuid, where uuid is the unique identifier of the company, stored as uuid in the company page’s front matter.

On each job listing, I want the logo of the company that posted it to be displayed. That means the job single.html template needs to get the image from the page bundle of the company with uuid referred to in the job front matter’s company: uuid key-value pair.

Can anyone provide a code snippet that will achieve this, please?

From your job single.html

{{ $company_uuid := .Params.company }}
{{ $companies := where site.RegularPages "Type" "company" }}
{{ with where $companies ".Params.uuid" $company_uuid }}
  {{ with index . 0 }}
    {{/* you got your company */}}
    {{ .Resources etc... }}
  {{ end }}
{{ end }}

Plenty of ways to achieve relationships in Hugo, but the above is the more popular and straight forward I suppose.

2 Likes

Wow, thank you for the fast reply! That did it :slight_smile:

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