Hexo: Output Posts by Category

Nexmoe April 15, 2021
This article is an AI translation and may contain semantic inaccuracies.

This article shows how to output posts under a specific category in Hexo.

Questions answered

https://www.zhihu.com/question/404351568

https://segmentfault.com/q/1010000017758828

Preface

The official helper function list_categories isn’t enough, so I used the categories variable directly.

I haven’t learned Node.js, so this is mostly trial and error.

I first logged site.categories.data with console.log(). It looks like this:

After some tinkering, I found this object is used via map().

Output category titles

<% site.categories.map(function(category){  %>
    <h1><%= category.name %></h1>
<% }) %>

Output post titles

<% site.categories.map(function(category){  %>
    <h1><%= category.name %></h1>
    <% category.posts.map(function(post){  %>
		<h2><%= post.title %></h2>
    <% }) %>
<% }) %>
VariableDescriptionType
post.titlePage titlestring
post.datePage creation dateMoment.js
post.updatedPage update dateMoment.js
post.commentsWhether comments are enabledboolean
post.layoutLayout namestring
post.contentFull page contentstring
post.excerptPage excerptstring
post.moreContent after the excerptstring
post.sourceOriginal source pathstring
post.full_sourceFull original source pathstring
post.pathPage URL (without root path). Often used as url_for(post.path) in themes.string
post.permalinkFull page URLstring
post.prevPrevious page. null if this is the first page.string or null
post.nextNext page. null if this is the last page.string or null
post.rawRaw post content???
post.photosPost photos (for albums)array
post.linkExternal link (for link posts)string