Created
June 20, 2019 18:22
-
-
Save tcrowe/340abc80346eeb3992337748c6a34578 to your computer and use it in GitHub Desktop.
hexo ejs hierarchical categories display
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<% | |
function displayCategories(parent = undefined) { // start with no parent | |
site.categories | |
.find({ parent }) // warehouse allows for complex queries | |
.sort("name") // sorting .sort("fieldName", 1) ascending or .sort("fieldName", -1) descending | |
.each(function(category) { | |
// count the child categories | |
const childCount = site.categories.find({ parent: category._id }).count(); | |
%> | |
<li> | |
<a href="<%=url_for(category.path)%>"><%=category.name%></a> | |
<% if (childCount > 0) { // recurse if there are child categories %> | |
<ul> | |
<% displayCategories(category._id); %> | |
</ul> | |
<% } %> | |
</li> | |
<% | |
}); | |
} | |
%> | |
<h3>Hierarchical category display</h3> | |
<ul> | |
<% displayCategories(); %> | |
</ul> |
Author
tcrowe
commented
Jun 20, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment