HUGO

How to insert a banner inside a list for posts

Hi there!
I am looking for a way to add a banner in the middle of a range used to show posts on the homepage. I am using Hugo Novela for this. I’d like it to be exactly in the middle of the post list (+ post without use of shortcodes)

Assuming your “posts” are in content/posts

{{ $pages := where .Site.RegularPages "Type" "posts" }}
{{ $first := div (len $pages) 2 }}
{{ $last := sub (len $pages) $first }}

{{ range first $first $pages }}
  <h2><a href="{{ .RelPermalink }}">{{ .Title }}</a></h2>
{{ end }}

<div>Put Your Banner Here</div>

{{ range last $last $pages }}
  <h2><a href="{{ .RelPermalink }}">{{ .Title }}</a></h2>
{{ end }}
1 Like

It’s also not uncommon to use modBool on the range index to do something every n item.

ok how would that work?

To display banner after every 5th item…

  {{ range $k, $v := .Site.RegularPages }}
    {{ if and $k (modBool $k 5) }}
      <div>Put Your Banner Here</div>
    {{ end }}
    <h2><a href="{{ .RelPermalink }}">{{ .Title }}</a></h2>
  {{ end }}
3 Likes

Thank you, this worked

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