List Recent Posts

Query and display recent content using Bengal's filters

1 min read 247 words

Display the most recent posts from a section using Bengal's query filters.

The Pattern

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
{% set posts = site.pages
  | where('section', 'blog')
  | where('draft', false)
  | sort_by('date', reverse=true)
  | limit(5) %}

<ul class="recent-posts">
{% for post in posts %}
  <li>
    <a href="{{ post.url }}">{{ post.title }}</a>
    <time>{{ post.date | date('%B %d, %Y') }}</time>
  </li>
{% endfor %}
</ul>

What's Happening

Filter Purpose
where('section', 'blog') Only pages in theblog/directory
where('draft', false) Exclude drafts
sort_by('date', reverse=true) Newest first
limit(5) Take only 5

Variations

From Any Section

1
2
3
4
5
{# All non-draft pages, any section #}
{% set recent = site.pages
  | where('draft', false)
  | sort_by('date', reverse=true)
  | limit(10) %}

With Featured Post

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
{% set posts = site.pages | where('section', 'blog') | sort_by('date', reverse=true) %}
{% set featured = posts | first %}
{% set rest = posts | offset(1) | limit(4) %}

<div class="featured">
  <h2>{{ featured.title }}</h2>
  <p>{{ featured.description }}</p>
</div>

<ul class="more-posts">
{% for post in rest %}
  <li><a href="{{ post.url }}">{{ post.title }}</a></li>
{% endfor %}
</ul>

Exclude Current Page

1
2
3
4
5
6
7
8
9
{# In a sidebar, show recent posts excluding the current one #}
{% set others = site.pages
  | where('section', 'blog')
  | sort_by('date', reverse=true)
  | limit(6) %}

{% for post in others if post.url != page.url %}
  <a href="{{ post.url }}">{{ post.title }}</a>
{% endfor %}

Seealso