I'm building a simple blog using Jekyll, and I'm pulling my hair out trying to figure out this problem.
The index page of the site is meant to feature a single, most recent article, with the structure looking something like below (pardon the mess):
{% for post in site.posts reversed limit:1 %}
<div class="post">
<div class="post-inner">
<h3 class="posttitle">{{ post.title }}</h3>
<p class="postdate">{{ post.date | date: "%d %B %Y" }}</p>
{{ post.content }}
</div>
</div>
{% endfor %}
The above template works perfectly fine when the limit is not restrictive (i.e does not exist or is set to the length of the array). It seems to only be when the limit is actually restricting the result that the loop ignores reversed.
I've tried clearing the browser cache, which is what got it working without limit:1, but the progress ends there.
Thanks for the help, and I would be happy to provide more detail if this is not enough.
What is Jekyll actually outputting for you?
From what I understand, the reversed filter is applied last. So let assume you posted the first 15 days of Aug, and lets also say you do something like this:
{% for post in site.posts limit:5 %}
{{ post.content }}
{% endfor %}
You post array would be ordered in the following pattern
[Aug 15, Aug 14, Aug 13, Aug 12, Aug 11]
And then if you reversed it
{% for post in site.posts reversed limit:5 %}
{{ post.content }}
{% endfor %}
You post array would be ordered in the following pattern
[Aug 11, Aug 12, Aug 13, Aug 14, Aug 15]
With all that said, I'm a little perplexed as to why you are not using
{% for post in site.posts limit:1 %}
Hack to have custom reversed sorting by custom field with limit:
{% assign items = site.items | sort: 'some_field' | reverse %}
{% for item in items limit:10 %}
<li>{{ item.title }}</li>
{% endfor %}
It's important to assign to some custom var, sort (and reverse) there and only then loop with limit.
Related
I have a folder of markdown files each with a number of key/values. I need to filter all of the markdown files in the _faq folder by the key faq_category.
I have tried:
{% assign post = site.faqs | where: "faq_category", name-of-category %}
<ul>
<li>{{ post.title }}</li>
</ul>
However, this is showing nothing in the end.
The folder structure it should be looping through is:
jekyll
|
--faqs
|
--name-of-faq
--name-of-faq-2
Sample markdown file:
title: name of faq
faq_id: 2567
slug: title-of-faq
created: Mar 6, 2017
modified: Mar 6, 2017
faq_category: how to fly
Instead of site.faqs use site.posts to get an array of posts.
Then put the markdown files in the folder: /faqs/_posts/ for example: /faqs/_posts/faq1.md.
After that you should be able to browse them like:
{% for post in site.posts %}
{{post.title}}
{% endfor %}
To filter a specific category use: site.categories.CATEGORY or filter them like: (for example the category "mycategory")
<ul>
{% for post in site.faqs %}
{% if post.categories contains "mycategory" %}
<li>{{ post.title }}</li>
{% endif %}
{% endfor %}
I moved from Jekyll pre-1.0 to 2.0 recently.
In my original code, on each blog post it will list all the title of posts that belongs to the same category as the current post being viewed. Previously this code worked:
{% for post in site.categories.[page.category] %}
<li {% if page.title == post.title %} class="active" {% endif %}>
{{ post.title }}</li>
{% endfor %}
However in the new version this does not work and I have to specify the category individually like so:
{% for post in site.categories.['NAME_OF_CATEGORY'] %}
Why can't I dynamically check for the category as before? And is there a work around for this instead of using if statements?
I figured it out. I had, in each post, my YAML front-matter category variables in uppercase or Camel case. Example: category: ABC or category: Zyx.
Doing page.category will always return the the actual category as it was written in the front-matter, which is ABC or Zyx. However site.categories.[CAT] only accepts CAT in lower cases (down case in liquid language).
Hence this will work site.categories.['abc'] or site.categories.['xyz'].
But this will fail site.categories.['ABC'] or site.categories.['Xyz']. It is the same as doing site.categories.[page.category].
Solution. Assign the current page category in lower case like so:
{% assign cat = page.category | downcase %}
{% for post in site.categories.[cat] %}
<li {% if page.title == post.title %} class="active" {% endif %}>
{{ post.title }}</li>
{% endfor %}
I’d like to list a fixed number of recent posts having the same category as the current post. This is what I have arrived at:
{% for category_name in page.categories limit:1 %}
<h2>Other articles in {{ category_name }}</h2>
<ul>
<!-- now what? -->
</ul>
{% endfor %}
I know about site.categories, but I don’t know how to subscript the dictionary. Obviously, site.categories.category_name is taken literally, looking for a category named “category_name”.
Based on the Jekyll documentation, indexing (i.e., [category_name]) is no longer the correct answer. Now (since at least Jekyll v2), given a category name FOO, the correct way to list all posts of that category is
{% for post in site.categories.FOO %}
<li>{{ post.title }}</li>
{% endfor %}
To note, I recently ran into this issue, my configuration is
$ jekyll -v
jekyll 2.0.3
{% for post in site.categories[category_name] %}
<li>{{ post.title }}</li>
{% endfor %}
This works for me:
{% for post in site.categories.FOO %}
+ [{{ post.title }}]({{ page.url }})
{% endfor %}
I'm looping through two products - on the post view page I pull in a secondary post (in the example, a related recipe) which parses just fine on the first product page - on the second product page just {{ post.content }} won't parse. I can hack it with {{ post.content | markdownify }} - but I'd like to know why it's breaking. Here's the relevant code:
{% for post in site.categories.recipe %}
{% if post.products contains page.title and post.featured %}
<div class="row">
<div class="four columns">
<h4>{{ post.title }}</h4>
<ul>
<li>Serves {{ post.serves }}</li>
<li>Prep: {{ post.time }}</li>
<li>Share</li>
</ul>
{{ post.content }}
...
<!-- All tags are closed, the rest just isn't relevant -->
{% endif %}
{% endfor %}
Please find my solution with counter
<pre>
{% assign counter=0 %}
{% for post in site.posts%}
{% if post.category == 'blog' and counter < 2 %}
{% assign counter=counter | plus:1 %}
{{post.content}}
{% endif %}
{% endfor %}
</pre>
The markdownify filter is probably making it work because there might be special characters that aren't encoded in the content you're pulling from. I always forget to make my & into &.
If you're using the default Markdown interpreter Maruku, here's a list of the entities that might be giving you problems and their encoded equivalent. http://maruku.rubyforge.org/entity_test.html and more info on Maruku. http://maruku.rubyforge.org/maruku.html
I'm trying to filter some post with jekyll.
I want to output all post with category: news.
It works fine when i do:
{% for post in site.posts %}
{% if post.category[0] == "news" %}
<h1>{{ post.title }}</h1>
{% endfor %}
but i'd like to limit the output to this filter to a number of posts.
If I apply a limit: 5 to my for loop it doesn't work as Jekyll applies the limit to the total number of posts.
Is it in anyway possible to apply a limit to an already filtered list of posts, something like:
{% for post in site.posts %}
{% if post.category[0] == "news" limit:5 %}
<h1>{{ post.title }}</h1>
{% endfor %}
I can get the list of categories with site.categories and list them
{% for category in site.categories %}
<p>test: {{ category[0] }}</p>
{% endfor %}
But i can't seem to be able to narrow to a single category.
I'm trying to do something like:
for post in site.categories.news limit:5
//do something
endfor
or
for post in site.categories['news'] limit:5
//do something
endfor
but to no avail. Is it possible to filter a category this way?
I manage to sort it out.
I couldn't access the filtered list of post through site.categories.news
I've added a tag: news on all the news page after looking through the jekyll bottstrap documentation.
I can now filter and limit the output of post with:
{% for post in site.tags.news limit:2 %}
//do something
{% endfor %}
My code just works. Looks like they fixed it somewhere since 2012.
{% for post in site.categories.company limit: 1 %}
...
{% endfor %}