使用 Liquid 按字母顺序对帖子进行排序
有没有办法使用 Jekyll 按字母顺序对多个帖子进行排序?
我现在有这样的东西:
{% for post in site.categories.threat %}
<li><a href="{{ post.url }}">{{ post.title }}</a></li>
{% endfor %}
它有效,但帖子很混乱。我想,如果按字母顺序排序,看起来会好得多。
谢谢
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我在本地站点测试了 Christian 的出色解决方案:在第一行之前输出中有一个空链接(我不知道为什么),因此第一个链接不起作用,所以我修改了他的代码插入
{% if postitems[1] %}
在{{ postitems[0] 行之前}}
和之后建议的{% endif %}
。 -display-posts-by-category">tanky woo 的评论。i tested Christian's great solution in my local site: before the first row there is an empty link (i don't no why) in output, therefore the first link doesn't work, so i modified his code inserting
{% if postitems[1] %}
before the line<a href={{ postitems[1] }}">{{ postitems[0] }}</a><br>
and an{% endif %}
after. suggested tanky woo's comment.如果没有插件或自定义功能,这是无法完成的。尽管如此,我们仍在努力在下一个版本中实现这一点:https://github.com /Shopify/liquid/pull/101 然后它看起来像:
另请参阅:使用 Jekyll / Liquid 模板订购阵列
It cannot be done without a plugin or custom function. Although, there is an ongoing effort to implement this in the next releases: https://github.com/Shopify/liquid/pull/101 and then it would look like:
See also: Order an array with Jekyll / liquid template
它可以无需插件即可完成,这意味着它可以与Github Pages配合使用。
不过,您必须使用一些丑陋的字符串操作技巧。
我使用了类似的方法来实现标签页面(列出每个标签的所有帖子)。
相同的方法,稍作修改:
注意:
第一个循环内需要两个不同的分隔符(当然,在稍后的
split 调用中也需要).
为了使其发挥作用,这两个字符都不能出现在任何帖子标题或 URL 中!
我使用的是
|
和#
在这个例子中,它对我有用(我刚刚用我的博客测试了它)。但您可能需要使用不同的字符,具体取决于您的帖子标题以及 URL 的构建方式。奖励:
如果您只想显示某个标签/类别中的帖子(而不是所有帖子),您可以更改第一个
for
循环(即在capture
中) 到以下之一:It can be done without a plugin, which means that it works with Github Pages.
You have to use some ugly string manipulation tricks, though.
I used a similar approach to implement a tag page (that lists all posts for each tag).
Same approach, slightly modified:
Beware:
You need two different separator characters inside the first loop (and of course again in the
split
calls later on).In order for this to work, both characters must not occur in any of the post titles or URLs!!
I'm using
|
and#
in this example, which works for me (I just tested it with my blog). But you might need to use different characters, depending on your post titles and how your URLs are constructed.Bonus:
If you want to display only the posts in a certain tag/category (and not all posts), you can change the first
for
loop (the one inside thecapture
) to one of these:根据文档,要按数组的字段之一过滤数组,可以使用:
然后
sortedPosts
变量将包含排序后的数组。该文档可以在这里找到:https://docs.shopify.com/主题/液体/过滤器/数组过滤器#sort
According to the documentation, to filter an array by one of its field, you can use :
Then the
sortedPosts
variable will contain the sorted array.The documentation can be found here : https://docs.shopify.com/themes/liquid/filters/array-filters#sort
无需插件即可在 GitHub 页面中对 Jekyll 进行排序,既干净又优雅。使用 _data 目录中的 .yml 数据文件。我在这里使用一个名为
team-members.yml
的数据文件:此模式将处理您需要在此处执行的操作。
It's both clean and elegant to sort in Jekyll in GitHub pages without a plugin. Use your .yml data file in the _data directory. I use a data file here called
team-members.yml
:This pattern will handle what you need to do here.
我改编了 https://gist.github.com/3812259 中的 Jekyll 插件来完成此任务。我无法按原样使用该插件,因为它在存在空值时失败。我是一名初级 ruby 程序员,并在 https://stackoverflow.com/a/808721/1135052< 的帮助下编写了空处理代码/a>
sort_for 例如反转排序并执行区分大小写的字符串比较(如果排序的属性不是字符串则忽略):
sorted_keys_for 例如:
用于Jekyll,将此代码放入 _plugins/sort_for.rb 中
I adapted a Jekyll plugin from https://gist.github.com/3812259 to accomplish this. I couldn't use the plugin as-is, because it failed in the presence of null values. I'm a beginning ruby programmer, and coded the null handling with help from https://stackoverflow.com/a/808721/1135052
sort_for example reversing the sort and performing case-sensitive string comparisons (ignored if the sorted property is not a string):
sorted_keys_for example:
For use in Jekyll, put this code in _plugins/sort_for.rb
我想添加以下内容以供将来参考。
要按标题对帖子进行排序,您可以使用
sort
过滤器。请参阅 http://jekyllrb.com/docs/templates/#filters
所以,这有效:
I wanted to add following for future reference.
To sort posts by title, you can use
sort
filter.See http://jekyllrb.com/docs/templates/#filters
So, this works: