简体   繁体   中英

Jekyll: Filter posts by subdirectory

I'm using jekyll to host a website on github.io

The homepage has a "Recent Posts" section which displays the latest 10 posts.

I'm using the following code in my index.html to generate the list:

<ul class="post-list">
{% for post in site.posts limit:10 %} 
  <li><article><a href="{{ site.url }}{{ post.url }}">{{ post.title }} <span class="entry-date"><time datetime="{{ post.date | date_to_xmlschema }}">{{ post.date | date: "%B %d, %Y" }}</time></span>{% if post.excerpt %} <span class="excerpt">{{ post.excerpt }}</span>{% endif %}</a></article></li>
{% endfor %}
</ul>

The _posts directory has 3 subdirectories. The above code generates a list from all three directories. I want to exclude the posts in one of those directories in my "Recent Posts" directory.

More generally, how can I select a subset of the subdirectories in the _posts directory?

I solved the problem by adding an if statement inside the for loop:

{% if post.path contains 'xxx' or post.path contains 'yyy'%}
  <li><article>...</article></li>
{% endif %}

This works for simple cases but it could get ugly for more complicated cases.

The for loop seems to be iterating over a list but I can't figure out how to generate a list with the elements I want. I can't find a good reference for the scripting language used by Jekyll.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM