使用液体按字母顺序排列帖子 [英] Using liquid to sort posts alphabetically

查看:187
本文介绍了使用液体按字母顺序排列帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我现在有这样的东西:


$ b有没有一种方法来按字母顺序排序一些帖子$ b

  {%for site.categories.threat%中的帖子} 
< li>< a href ={{post.url}}> {{post.title}}< / a>< / li>
{%endfor%}

有用,但帖子混乱。如果按字母顺序排序,我认为它会更好看。



谢谢

解决方案

可以在没有插件的情况下完成,这意味着它可以与 Github Pages

但是,您必须使用一些丑陋的字符串操作技巧。

我使用了类似的方法
要实现一个标签页(列出每个标签的所有帖子)

同样的方法,稍微修改:

  {%capture posts%} 
{site.posts%中的帖子%}
| {{post.title}}#{{post.url}}
{%endfor%}
{%endcapture%}
{%assign sortedposts = posts | split:'|'| sort%}
{%for postpostpostposts%}
{%assign postitems = post | split:'#'%}
< a href = {{postitems [1]}}> {{postitems [0]}}< / a>< br>
{

在第一个循环中需要两个不同的分隔符(当然在后面的 split >调用中也是这样)
为了达到这个目的,这两个字符都不能出现在任何帖子标题或URL中!

我在这个例子中使用了 | ,这对我很有用(我刚测试过我的博客)。但是您可能需要使用不同的字符,具体取决于您的帖子标题以及您的网址是如何构建的。




奖励:



如果您只想显示特定标签/类别(而不是所有帖子)中的帖子,您可以将 loop capture )中的第一个更改为这些:

<$ p $ %>
$ b {%for site.categories ['whatever']%}


Is there a way to sort a number of posts alphabetically, using Jekyll?

I have something like this now:

{% for post in site.categories.threat %}
<li><a href="{{ post.url }}">{{ post.title }}</a></li>
{% endfor %}

It works, but the posts are jumbled up. Would look much nicer if they were sorted alphabetically I think.

Thanks

解决方案

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:

{% capture posts %}
  {% for post in site.posts %}
    |{{ post.title }}#{{ post.url }}
  {% endfor %}
{% endcapture %}
{% assign sortedposts = posts | split: '|' | sort %}
{% for post in sortedposts %}
    {% assign postitems = post | split: '#' %}
    <a href={{ postitems[1] }}">{{ postitems[0] }}</a><br>
{% endfor %}

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 the capture) to one of these:

{% for post in site.tags['whatever'] %}

{% for post in site.categories['whatever'] %}

这篇关于使用液体按字母顺序排列帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆