List posts by category in Hexo

Spend hours to figure it out the documentation is not very helpful!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<% site.categories.each(function(cat) { %>
<section class="archives-wrap">
<div class="archive-year-wrap">
<p class="article-title" style="font-size: 20px;"><%= cat.name %></p>
</div>
<div class="archives">
<% var postCount = 0 %>
<% cat.posts.sort('-date').limit(4).each(function(post) { %>
<% postCount++; %>
<% if (postCount % 2 == 1) { %>
<div class="article-row">
<% } %>
<%- partial('summarynocategory', { post: post }) %>
<% if (postCount % 2 == 0) { %>
</div>
<% } %>
<% }) %>
</div>
</section>
<% }) %>

and here is the summarynocategory.ejs:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<article class="article article-summary">
<div class="article-summary-inner">
<% if (theme.customize.thumbnail === true) { %>
<%- partial('common/thumbnail', { counter: true }) %>
<% } %>
<div class="article-meta">
<p class="date"><time datetime="<%= date_xml(post.date) %>" itemprop="datePublished"><%= date(post.date) %></time></p>
</div>
<%- partial('post/title', { class_name: 'article-title', linkable: true }) %>
<p class="article-excerpt">
<%- excerpt(post) %>
</p>
</div>
</article>
Share