This article will teach you how to call articles under a specified category in Hexo.
This article answers the following questions#
https://www.zhihu.com/question/404351568
https://segmentfault.com/q/1010000017758828
Introduction#
The official helper function list_categories is not sufficient, so I had to use the variable "categories" myself.
Since I haven't learned node.js, I had to experiment blindly.
First, I used console.log() to check the content of site.categories.data, which is as follows.
After some experimentation, I found that this object is called using map().
Output category titles#
<% site.categories.map(function(category){ %>
<h1><%= category.name %></h1>
<% }) %>
Output article titles#
<% site.categories.map(function(category){ %>
<h1><%= category.name %></h1>
<% category.posts.map(function(post){ %>
<h2><%= post.title %></h2>
<% }) %>
<% }) %>
Variable | Description | Type |
---|---|---|
post.title | Page title | string |
post.date | Page creation date | Moment.js object |
post.updated | Page update date | Moment.js object |
post.comments | Whether comments are enabled | boolean |
post.layout | Layout name | string |
post.content | Full content of the page | string |
post.excerpt | Page excerpt | string |
post.more | Remaining content after the excerpt | string |
post.source | Original path of the page | string |
post.full_source | Full original path of the page | string |
post.path | Page URL (without the root path). We usually use url_for(post.path) in themes. | string |
post.permalink | Full URL of the page | string |
post.prev | Previous page. If this is the first page, it is null . | string or null |
post.next | Next page. If this is the last page, it is null . | string or null |
post.raw | Raw content of the article | ??? |
post.photos | Photos in the article (for albums) | array |
post.link | External link of the article (for linked articles) | string |