0

In Hexo.js, when you want to output some articles, you loop using .sort, .limit and .each, for example:

<% site.posts.sort('date', 'desc').limit(8).each(function(post){ %>
    <div id="post-1" class="post">
        <%= post.title %>
        all the other post tags and content
    </div>
<% }) %>

How do you set the id number post-X to be dynamically incremented, for example first post would get id="post-1", second id="post-2" and so on?

revelt
  • 2,312
  • 1
  • 25
  • 37

1 Answers1

1

Try this:

<% site.posts.sort('date', 'desc').limit(8).each(function(post, i){ %>
    <div id="post-<%=i+1%>" class="post">
        <%= post.title %>
        all the other post tags and content
    </div>
<% }) %>

As you can see, there is an additional parameter, i, which means index.

ntalbs
  • 28,700
  • 8
  • 66
  • 83