How to solve a common problem for blogs ("featured post ends up among latest")

Hello everyone,

There are a few major blogs/news sites that run on Static Site Generators, most notably Vox.com (using Middleman). As a few forum users have noted I ran into a classic problem the other day.

A blog usually has featured posts at the top (and on other places on homepage and sections) while also listing other posts underneath the featured post in the following order:

  • Featured post
  • Newest 10 posts
  • Paginator

So how can one exclude the featured post from also ending up in the “newest 10 posts”?

Using special tags (taxonomy) is not a great solution since one might want to list all tags somewhere. We also want to make sure that a post featured on the homepage could still be listed as “not featured” elsewhere. Below is my solution.

Frontmatter in posts

+++
(your own standard frontmatter here)
type = "post"
featloc = "start"
+++

If you have several featured posts on the start page, featloc = "start" could be extended to featloc = "start-top" or similar.

Ranges

# Featured post
{{ range first 1 (where .Site.Pages.ByDate.Reverse "Params.featloc" "start-top") }}
    <a href="{{ .Permalink }}"><h1>Featured 1 — {{ .Title }}</h1></a>
{{ end }}

# Newest/latest posts
{{ range (.Paginate (where .Data.Pages.ByDate.Reverse "Type" "post")).Pages }}
    {{ if not (in .Params.featloc "start")}}
        <li><a href="{{ .Permalink }}">{{.Title}}</li></a>
    {{ end }}
{{ end }}
{{ partial "pagination.html" . }}

This ensures that the featured post doesn’t end up in the list of newest/latest posts. The code works on sections as well, but here it’s a good idea to use another value in featloc that can be excluded on the if not in line.

Finally, it could be even cleaner if we didn’t need to use type = "post". But using Sections my range also listed _index.md-files. Well, nothing’s perfect.

Cheers!

1 Like

Sorry if I keep bumping this to the top guys. But I also thought it was worth mentioning @bep’s solution for using a different template for the first result in a range.

1 Like