Let’s say you want to ex­clude a post from your RSS feed. How do you go about do­ing that? Well, there are a few ways. First, you’d need to add some­thing to the front mat­ter of your post:

# 01-01-2017-test-post.md
title: Test Post
exclude_rss: true

We can use the exclude_rss vari­able to then fil­ter out posts we don’t want in our feed.

Solution One: if Statement #

Open up your feed file, mine is called atom.xml.

<!-- atom.xml -->
{% for post in site.posts %}
{% if post.exclude_rss != true %}
<!-- entry stuff goes in here -->
{% endif %}
{% endfor %}

This so­lu­tion def­i­nitely works, and if your cir­cum­stances aren’t very com­plex, you’d be to­tally fine to use this. But this so­lu­tion breaks down quickly if you want only a cer­tain type of post in your feed.

Solution Two: where and where_­exp Filter #

Let’s in­tro­duce some com­plex­ity into our prob­lem. Let’s say we want to limit our feed to twenty en­tries where the only en­tries are ar­ti­cles, and we can still fil­ter out spe­cific posts.

We’ll use the where and where_exp fil­ter. You can read a lit­tle bit more about them in the Jekyll Docs. The rea­son we need these more ad­vanced fil­ters is be­cause if we’re cre­at­ing an ar­ti­cles-only feed with twenty en­tries, we’d want twenty en­tries of ar­ti­cles. If we were to use the if state­ment method, we would­n’t get twenty en­tries of ar­ti­cles if in the last twenty en­tries only two were ar­ti­cles. That would only give us two en­tries.

Again, we’ll open up our feed file. The fol­low­ing is what mine looks like, but mod­ify as you see nec­es­sary.

<!-- atom.articles.xml -->
{% assign posts = site.posts | where_exp:"post", "post.exclude_rss != true" | where:"custom_type","post" %}
{% for post in posts limit:20 %}
<!-- entry stuff here -->
{% endfor %}

This code tells our for loop to only it­er­ate on posts where post.exclude_rss is not equal to true and where the custom_type is post. This method gets us twenty en­tries of ar­ti­cles and ex­cludes posts we don’t want in the feed.

I love this about Jekyll: there a few dif­fer­ent ways of do­ing things de­pend­ing on how com­plex you want to get.

Series Hacking Jekyll

As I learn things about Jekyll, I try to write about them.

View Series →

Up Next