【问题标题】:Sorting Jekyll tags by number of posts按帖子数量对 Jekyll 标签进行排序
【发布时间】:2021-09-20 19:09:07
【问题描述】:

下面给出了显示我正在使用的 Jekyll 主题标签的代码。它目前按字母顺序显示标签。我应该更改什么以通过帖子计数进行排序?我看到了a similar question,但他们使用的代码与这个主题中的代码非常不同。

{% capture site_tags %}{% for tag in site.tags %}{{ tag | first | downcase }}{{ tag | first }}{% unless forloop.last %}{% endunless %}{% endfor %}{% endcapture %}
{% assign site_tags = site_tags | split: '' | sort %}
{% capture site_tags %}{% for tag in site_tags %}{{ tag | split: '' | last }}{% unless forloop.last %}{% endunless %}{% endfor %}{% endcapture %}
{% assign site_tags = site_tags | split: '' %}

{% assign post_count = site.posts | size %}
{% capture tag_counts %}{% for tag in site_tags %}{{site.tags[tag] | size | times: 10 | plus: post_count | minus: 1 | divided_by: post_count }}{% unless forloop.last %},{% endunless %}{% endfor %}{% endcapture %}
{% assign tag_counts = tag_counts | split: ',' %}

<ul class="tags quick-links">
{% for tag in site_tags %}
    {% assign tag_data = site.data.tags[tag] %}
    {% capture tag_classes %}tag tag-size-{{ tag_counts[forloop.index0] }} tag-{{ tag | slugify }}{% endcapture %}
    {% capture tag_name %}{% if tag_data.title %}{{ tag_data.title }}{% else %}{{ tag }}{% endif %}{% endcapture %}
    <li><a href="{{ include.baseurl }}#{{ tag | slugify }}" class="{{ tag_classes }}">{{ tag_name }}<span class="count">{{ site.tags[tag] | size }}</span></a></li>
{% endfor %}
</ul>

the file 的链接。这是我用的theme

截图:

【问题讨论】:

  • 你能分享正在生成的带有截图的html吗?
  • @Chandan 我添加了截图。
  • 能不能也分享一下生成的html
  • @Chandan view-source:iamsreeman.github.io/blog
  • 只是为了确认你想要标签为Philosophy[2], Quantum mechanics[2],Textbook[2],Atheism[1],Ethics[1],....

标签: ruby sorting jekyll


【解决方案1】:

{{ tag | first | downcase }} 更改为 {{ tag[1].size | plus: 1000 }} 以使用帖子计数进行排序。

剩下的问题是帖子会以升序出现,可以通过在标签排序后添加reverse来反转顺序来轻松解决。

{% capture site_tags %}
  {% for tag in site.tags %}
    {{ tag[1].size | plus: 1000 }}&#x1F;{{ tag | first }}
    {% unless forloop.last %}&#x1E;{% endunless %}
  {% endfor %}
{% endcapture %}
{% assign site_tags = site_tags | split: '&#x1E;' | sort | reverse %}

【讨论】:

  • 它正在工作。但它实际上显示的是反向字母顺序和递减的帖子编号。但对我来说没关系,即使它显示逆字母顺序。
  • @KasiReddySreemanReddy 我已经更新了解决方案,请检查并告诉我是否有效。
  • 我只使用了那个。即使您最初写的是split: '&amp;#x1E;' | reverse %},我还是使用了split: '&amp;#x1E;' | sort | reverse %},因为第一个不能正常工作。
猜你喜欢
  • 2014-12-06
  • 2022-10-03
  • 2014-09-02
  • 1970-01-01
  • 2021-11-10
  • 1970-01-01
  • 1970-01-01
  • 2021-07-26
  • 2014-08-15
相关资源
最近更新 更多