Filtering posts in Jekyll - jekyll

I have a newspaper running on Jekyll. On the home page there’s a bunch of the most recent posts and below them there are posts grouped by various interesting topics. For example:
Current Headlines (site.posts[1..9])
Opinions (site.categories['opinions'])
Culture (site.tags['culture'])
…
Now, when rendering the opinions, I don’t want to repeat the stories that are already listed under Current Headlines. Is there an easy way to express that in Liquid, without any Jekyll plugins?

Loop through all posts with an offset and use an if statement for the category. Offset can be found here: https://help.shopify.com/themes/liquid/tags/iteration-tags
{% for post in site.posts offset:9 %}
{% if post.categories contains 'mycat' %}
{{ post.title }}
{% endif %}
{% endfor %}

Related

Category Page without plugins

currently I have a Jekyll based site. The site was created about three years ago, but currently I have problems updating it. On the Site I have a blog. And in the navigation I have category pages for the blog.
To create a page for each category, I created a layout, where the layout is showing the posts for the matching category.
So I have a Page without any content for the category:
---
layout: category
title: Elektronik
category: elektronik
permalink: /blog/elektronik/index.html
---
And the Layout for category, where I go through the postings:
{% for post in site.categories.[page.category] %}
...
{% endfor %}
This solution worked before, but now the category page is clear and I get an error message during compilation:
Liquid Warning: Liquid syntax error (line 36): Expected id but found open_square in "post in site.categories.[page.category]" in /_layouts/category.html
How can I solve it?
You can use 'contains' in your for loop, like this:
{% for post in site.posts %}
{% if post.categories contains page.category %}
...
{% endif %}
{% endfor %}

Get page variable from post variable in Jekyll?

I'm iterating over all the posts in my site like so
{% for post in site.posts %}
// code
{% endfor %}
I want to access some variable that I have stored at the page level. How can access it? I wasn't able to find anything after googling for awhile. I want to do something like
{% for post in site.posts %}
post.page.special_var
{% endfor %}
Jekyll support both post and page, so it is depend on you, which type of variable you want to access.
For example here is your post front matter.
---
layout:post
title: jekyll test
categories: jekyll
---
So in head.html, I am using this.
<title>{% if page.title %}{{ page.title }}{% endif %}</title>
I am using page to access that variable because there are too many pages like about or contact or privacy policy that does not belongs to jekyll post,so there you can't use post for example post.title to access that variable.
Now, look out these codes
{% for post in site.categories.jekyll reversed limit:10 %}
<span><a href="{{ post.url }}">{{ post.title}}<a/></span>
{% endfor %}
Here you note that, I am using loop, because I want to access same variable from multiple post, and that variable was jekyll .
Here I am using post.title but you can even use page.title, because it is globally accessible.
For fun :
I am using reversed, so you can order your post in which date you are created, older post will show at first.
I am using limit:10 because, I want to show only 10 post per page.
If you have a special_var variable defined in a post front matter you can get it like this :
{% for post in site.posts %}
<p>This is my var {{ post.special_var }}.</p>
{% endfor %}
See Jekyll documentation here.

How to show first post from category in Jekyll with Liquid

I can't find a solution. I have three categories: tuts, news, code.
The newest post is categorized in tuts. But I want to show the last and newest post in news. I tried the following, but obviously it doesn't show anything, because if I limit the loop to the first item, which is the tuts item, the loop stops.
{% for post in site.posts limit:1 %}
{% if post.categories contains 'news' %}
NEWS</strong> › {{ post.title }}
{% endif %}
{% endfor %}
How do I show the first posting from a special category? Can I loop directly through a chosen category like this? If yes, what is the correct syntax?
{% for post in site.posts.categories.news limit:1 %}
NEWS</strong> › {{ post.title }}
{% endfor %}
Yes, it's possible to directly loop all posts for a certain category or tag.
It's just:
{% for post in site.categories['news'] limit:1 %}
{{ post.title }}
{% endfor %}
It's the same for tags, you just have to replace site.categories['news'] by site.tags['news'].

what are some creative ways to overcome jekyll's default 'reverse chronological order' listing?

i've got a static content site and I actually don't want articles display in reverse chronological order, using jekyll/liquid, what are some creative ways I can accomplish this without having to revert to reverse ordering the dates on all posts ?
With some ugly looking Liquid, it's possible to sort by something else.
Here's an example how to create a tag page, with alphabetically sorted tags.
In this example, I'm sorting the tags (and then the posts per tag are sorted in reverse chronological order - I didn't change that).
But you could use the same technique to order the posts by title or URL, for example.
EDIT:
If you just want to list your posts in forward chronological order instead of the default reverse chronological order, there's a much, much simpler solution - the reversed keyword:
{% for post in site.posts reversed %}
<!-- whatever -->
{% endfor %}
To list your Posts by Category, you could do the following:
{% for category in site.categories %}
<h2>{{ category[0] }}</h2>
<ul class="posts">
{% for post in category[1] %}
<li><span>{{ post.date | date_to_string }}</span> » {{ post.title }}</li>
{% endfor %}
</ul>
{% endfor %}
This would yield something similar to this:
Note that with this method, Posts are still listed in reverse chronological order within each category. You can see this code in action here.

How to list the newest 10 posts using liquid?

I establish my Blog with the help of jekyll bootstrap and deploy it on github. Here is my question:
I want to add a sidebar which lists my newest 10 posts. When I use the code below, it lists all my posts:
<ul>
{% for post in site.posts %}
li>{{ post.title }}</li>
{% endfor %}
</ul>
BUT, I only want to list the newest 10 posts(if the num of all posts less than 10, list all),how can I do?
Thank You for your answer!
I don't have the environment to test it, but you might want to try limit keyword, see documentation here. I assume it will show all if limit is not reached.
<ul>
{% for post in site.posts limit:10 %}
<li>{{ post.title }}</li>
{% endfor %}
</ul>