有没有办法从site.posts获取当前的帖子索引号?如何检索Jekyll中的当前帖子索引号?
{{site.posts |大小}}是帖子的总数。 我需要的是类似{{site.posts.index}}或{{page.index}}。
我试图在每个帖子页面上显示一个计数器。例如:后的2654
有没有办法从site.posts获取当前的帖子索引号?如何检索Jekyll中的当前帖子索引号?
{{site.posts |大小}}是帖子的总数。 我需要的是类似{{site.posts.index}}或{{page.index}}。
我试图在每个帖子页面上显示一个计数器。例如:后的2654
在for
环43,你可以通过两种方式获得当前项目指标:
{% for post in site.posts %}{{ forloop.index }}{% endfor %}
# will print 123...
或
{% for post in site.posts %}{{ forloop.index0 }}{% endfor %}
# will print 012...
而且你需要的是{{ forloop.index }}
(回答我自己的问题,也许它可以帮助别人)
确实存在使用简单的哲基尔插件另一种方式(和没有出现大的性能损失):
module Jekyll
class PostIndex < Generator
safe true
priority :low
def generate(site)
site.posts.each_with_index do |item, index|
item.data['index'] = index
end
end
end
end
另存为post_index_generator.rb,地点在_plugins文件夹。
获取模板后指数与{{page.index}}
它的工作原理,但循环为每一个岗位的所有帖子查找索引是太昂贵,使代比登天长不幸。任何其他想法?任何方式以编程方式将索引号添加到前面的问题可能? – deadelvis 2014-09-28 03:29:46