Range first 4 posts that matches the first value inside the Param's array

Hi there,

I have a portfolio single template, that would like to render the first 4 related portfolio with the first value of the industry which is health.

Given inside:

# content/works/work-1.md
industry: ["health", "lifestyle"]
# content/works/work-2.md
industry: ["health", "food"]
# content/works/work-3.md
industry: ["health", "retail"]
# content/works/work-4.md
industry: ["health"]

HTML inside layouts/work/single.html:

<div class="stories-list masonry stories-new">
{{$industryCat := index (.Site.Params.industry) 0 }}
{{ range first 4 (where .Site.RegularPages "Section" "works") $industryCat  }}
  <div class="story-item grid-item blue">
    <div class="img">
      <img src="uploads/img29.jpg" alt="image description">
    <h3><a href="#">Title</a></h3>
    <p><a href="#">Description</a></p>
    <ul class="categories">
      <li><a href="#">Health</a></li>
{{ end }}

Thank you

There’s no where statement that will allow you to do that. You will have to range on your posts and select the one you want:

{{ $selected := slice }}
{{ range $entry :=  where .Site.RegularPages "Section" "works" }}
  {{ with .Params.industry }}
    {{ if eq (index . 0) "health" }}
       {{ $selected = $selected | append $entry }}
    {{ end }}
  {{ end }}
{{ end}}
  1. Initiate $selected an empty slice.
  2. Store the entry at cursor as $entry (same as {{ range $entries}} {{ $entry := . }}...
  3. Check if .Params.industry is set
  4. Check if first item of .Params.industry is “health”.
  5. Append the entry to your list of selected…
  6. Use $selected as you wish, with first 4 $selected or else…