How to show the first post of a category in Jekyll with Liquid

I can not find a solution. I have three categories: tuts, news, code.

The latest publication is divided into categories. But I want to show the latest and newest news post. I tried the following, but obviously it shows nothing, because if I restrict the loop to the first element, which is the tuts element, the loop stops.

{% for post in site.posts limit:1 %}
    {% if post.categories contains 'news' %}
        <a href="{{ site.url }}/news/">NEWS</a></strong> › <a href="{{ site.url }}{{ post.url }}">{{ post.title }}</a>
    {% endif %}
{% endfor %}

How to show the first publication from a special category? Can I scroll directly to a selected category? If so, what is the correct syntax?

{% for post in site.posts.categories.news limit:1 %}
        <a href="{{ site.url }}/news/">NEWS</a></strong> › <a href="{{ site.url }}{{ post.url }}">{{ post.title }}</a>
{% endfor %}
+4
source share
1 answer

Yes, it is possible to directly connect all messages for a specific category or tag.

It's simple:

  {% for post in site.categories['news'] limit:1 %}
    <a href="{{ post.url }}">{{ post.title }}</a>
  {% endfor %}

, site.categories['news'] site.tags['news'].

+6

Source: https://habr.com/ru/post/1548371/


All Articles