1

On my Jekyll website, I have an overview page on which I list my last 10 blog posts.

However, I also assign a tag exclude to some of my blog posts and those I don't want to show. This works, but then I don't get the last 10 blog posts, but 10 minus the number of exclude blog posts.

Here is how that looks like:

---
layout: page
title: "Last posts"
permalink: /last/
---

### Last posts

{% for post in site.posts limit:10 %}
    {% unless post.category == "exclude"%}
      * {{ post.date | date_to_string }} » [ {{ post.title }} ]({{ post.url }})
    {% endunless %}
{% endfor %}

How can I always show the last 10 non-exclude blog posts?

marcanuy
  • 23,118
  • 9
  • 64
  • 113
ulima2_
  • 1,276
  • 1
  • 13
  • 23

1 Answers1

1

To show the last 10 non-exclude blog posts:

  1. Create an array with posts that doesn't contain the exclude tag.

    {% assign nonexcludeposts = ''|split:''%}
    {% for post in site.posts %}
        {% unless post.category == "exclude"%}
        {% assign nonexcludeposts = nonexcludeposts|push:post%}
        {% endunless %}
    {% endfor %}
    
  2. Display 10 most recent posts

    <ul>
    {% for post in nonexcludeposts limit:10 %}
          <li>
          <a href="{{post.url|absolute_url}}">{{post.title}}</a>
          </li>
    {% endfor %}
    </ul>
    
marcanuy
  • 23,118
  • 9
  • 64
  • 113
  • It extracts the posts and the right number of them, but this list does not render properly for me. Maybe I'm including this wrong. On the page it then displays: * 08 Aug 2017 » [ My great post ](/2017/08/08/my-great-post/) * 28 Jul 2017 » [ Another good post ](/2017/07/28/another-good-poost/) ... [the other posts] ... – ulima2_ Aug 14 '17 at 12:39
  • I've used the original code in that part because it wasn't related to the question, to generate hyperlinks for each post use replace it with `{{post.title}}`. – marcanuy Aug 14 '17 at 12:42