Hello,
How can I get a specific post from a category?
Right now I can only use a specific post from all the posts on the site:
{% assign post = site.posts [1]%}
I also need to take 2,3,4 posts from the news category
No loop and offset
Hello,
How can I get a specific post from a category?
Right now I can only use a specific post from all the posts on the site:
{% assign post = site.posts [1]%}
I also need to take 2,3,4 posts from the news category
No loop and offset
---
title: My title
categories: foo bar
---
I don’t know why you don’t want a loop, it seems odd without the context, but anyway:
{% assign c = site.categories.foo %} <!-- posts in category "foo" -->
- c[1].title <!-- Second item at index 1 -->
- c[2].title
- c[3].title
slice
You can get the first 3 posts in foo
like:
{% assign c = site.categories.foo | slice: 0, 3 %}
{% for p in c %}
- {{ p.title }}
{% endfor %}
Add a link!
{% for p in c %}
- [{{ p.title }}]({{ p.url | relative_url }})
{% endfor %}
Or start at 2nd item and get 3 items.
{% assign c = site.categories.foo | slice: 1, 3 %}
{% for p in c %}
- {{ p.title }}
{% endfor %}
Or if you don’t want to use slice
. Here also getting first 3 items starting at 2nd.
{% for p in site.categories.foo offset: 1 limit: 3 %}
- {{ p.title }}
{% endfor %}
For interest to explore all items in all categories. From my Cheatsheet
<ul>
{% for c in site.categories %}
<li>
<h3>
{{ c[0] }}
</h3>
<ul>
{% for p in c[1] %}
<li>
<a href="{{ p.url | relative_url }}">
{{ p.title }}
</a>
</li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
Result
Thanks a lot.
The first option came in handy for me!
The cycle is not used, since posts are displayed in the layout, where the post blocks are different from each other.