1

I would like to group categories in hexo. While the hexo <% list_categories %> helper lists all my categories alright, I would like to group nested categories right.

My question is on two levels, first, how do you represent the subcategories in the front matter. In other words, how would I add the subcategories [motosport, cricket, basketball, hockey] into the following front matter. sports is the main category

categories:
  - sports

Two, how do I enumerate through all categories and each of their children. Do I still use this code

<%- list_categories(site.categories, { options}); %>

or is there a better way/function?

One
  • 273
  • 1
  • 3
  • 13
  • This theme support childCategories you could look how it's done https://github.com/LouisBarranqueiro/hexo-theme-tranquilpeak/blob/master/layout/all-categories.ejs – Dylan Delobel Sep 13 '17 at 12:18
  • I checked it, and I still have a problem because there is no actual example of how you hook into that support in an actual project. I mean, how do you write the subcategories in the front matter? – One Sep 13 '17 at 13:22
  • Try `parent-categories: MyCategory` in front matter on Categories-filter.js `this.dataCategory = 'category';` `this.dataParentCategories = 'parent-categories';` – Dylan Delobel Sep 13 '17 at 13:45
  • Hmm, in the yaml. Look I want to publish an article under **category** sports, and **subcategory** cricket. How would you put it? – One Sep 13 '17 at 13:51
  • Then how would you reference this in the theme so that it works for all posts? – One Sep 13 '17 at 13:53

1 Answers1

5
  1. In Hexo v3.3.9 added hierarchical categories feature. Need to update hexo/node_modules/hexo/lib/models/post.js by this commit (or newer if exists). And set categories like this (in *.md):

    categories:
    - [sport, motosport]
    - [sport, cricket]
    - [sport, basketball]
    - [sport, hockey]
    
  2. <%- list_categories() %> without any parameters enumerate parents and their childs already in hierarchical list; <%- list_categories(site.categories, {depth: 1}) %> enum only parents (sport) of site total, etc. Full options for now here.
Ivan Nginx
  • 111
  • 1
  • 1
  • 5