I made a dummy "related posts" in Jekyll 3.2.1 with the following solution:
{% for post in site.posts limit: 4 %}
{% if page.author == post.author and page.title != post.title %}
<div class="post-stream-item">
<a href="{{ post.url | prepend: site.baseurl }}" title="{{ post.title }}"><div class="post-stream-content">
<img src="{{ post.thumbnail }}" width="80" height="auto" /><span class="post-stream-item-meta"><h3>{{ post.title }}</h3><p>{{ post.author }} on {{ post.date | date: "%b %-d, %Y" }} • {% assign words = post.content | number_of_words %}
{% if words <= 160 %}
1 min
{% else %}
{{ words | plus: 159 | divided_by:160 }} mins
{% endif %} read</p></span></div></a></div>{% if forloop.last == false %}<hr>{% endif %}
{% endif %}
{% endfor %}
The problem is with the {% if forloop.last == false %}<hr>{% endif %} part, cause if there is more iterable (post) in the forloop, it will display the <hr> tag, even if it's the last element shown to the user.
Is there any attribute to refer to the penultimate element of the list or any better solution to this?
There isn't going to be a simple out-of-the-box one-line solution for this. Think about it this way: you're essentially asking for a feature that looks ahead in time and figures out whether this is the last time the if statement will evaluate to true. Jekyll is great, but it can't predict the future!
You could do this yourself by using two loops: one that loops through and counts how many <hr> elements you should show. Then another that actually prints stuff out, checking against the count you came up with to decide whether to print the <hr> element.
Or you could just use CSS to hide the last <hr> element. Google is your friend here.
Solution : use loop limit
{% for post in site.posts limit: 4 %}
... output code here
{% endfor %}
You will print exactly 4 posts and forloop.last always works.
Solution : use where filter, a counter and break
Now that you include a conditional printing :
If you want to know how many posts you can print, you can use {% assign authorsPosts = site.posts | where: "author", page.author %} and authorsPosts.size.
This code will do it nicely, even if available posts number is less than your limit.
{% comment %} +++++ Max number of posts to print +++++ {% endcomment %}
{% assign limit = 4 %}
{% comment %} +++++ Select authors posts +++++{% endcomment %}
{% assign authorsPosts = site.posts | where: "author", page.author %}
{% comment %} +++++ If author's Posts number is less than limit, we change the limit +++++ {% endcomment %}
{% if limit >= authorsPosts.size %}
{% comment %} +++++ Number of "listable" posts is author's posts number less 1 (the one actually printed) +++++ {% endcomment %}
{% assign limit = authorsPosts.size | minus: 1 %}
{% endif %}
{% assign postsCounter = 0 %}
{% for post in authorsPosts %}
{% if page.author == post.author and page.title != post.title %}
{% assign postsCounter = postsCounter | plus: 1 %}
<h3>{{ post.title }}</h3>
{% comment %} +++++ Prints hr only if we are not printing the last post +++++ {% endcomment %}
{% if postsCounter < limit %}<hr>{% endif %}
{% comment %} +++++ Exit for loop if we reached the limit +++++ {% endcomment %}
{% if postsCounter == limit %}{% break %}{% endif %}
{% endif %}
{% endfor %}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With