2014-09-27 80 views
1

有沒有辦法從site.posts獲取當前的帖子索引號?如何檢索Jekyll中的當前帖子索引號?

{{site.posts |大小}}是帖子的總數。 我需要的是類似{{site.posts.index}}{{page.index}}

我試圖在每個帖子頁面上顯示一個計數器。例如:後的2654

回答

2

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 }}

+1

它的工作原理,但循環爲每一個崗位的所有帖子查找索引是太昂貴,使代比登天長不幸。任何其他想法?任何方式以編程方式將索引號添加到前面的問題可能? – deadelvis 2014-09-28 03:29:46

4

(回答我自己的問題,也許它可以幫助別人)

確實存在使用簡單的哲基爾插件另一種方式(和沒有出現大的性能損失):

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}}