Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jekyll for loop - multiple arrays

Tags:

jekyll

In Jekyll, I have a small blog system going with two categories: blog and portfolio. I have two pages that will render the posts for these categories, with a for loop that looks something like this:

{% for post in site.categories['blog'] %}
…
{% endfor %}

On my home page, I want to show the latest post in the blog category. However, I want my blog to contain some of my photography too - these will exist as posts, but most of the time they have no title or body content - they are just an image. I do not want those posts to show up on the home page.

Effectively, I only want the latest blog post to show up on the home page, excluding all photography posts. I am considering my options for this:

  1. Use two categories: blog and photos. However, how would I then make my blog page show posts from both of these categories? I would effectively need to merge the arrays site.categories['blog'] and site.categories['photos'], and put those into a for loop - but is that possible?
  2. Keep one category: blog, but in some other way, exclude posts without a title from appearing on the home page. But how do you do that?

Is this possible?


1 Answers

On the blog page you can use:

{% for post in site.posts %}
…
{% endfor %}

To retrieve all posts and show them all. If you want to filter more (say, you have a third category you wouldn't want to show on this page:

{% for post in site.posts %}
    {% if post.categories contains 'post' or post.categories contains 'photos' %}
       ...
    {% endif %}
{% endfor %}
like image 176
Lolindrath Avatar answered Oct 01 '26 19:10

Lolindrath



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!