Skip to content

Instantly share code, notes, and snippets.

@tcrowe
Created June 20, 2019 18:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tcrowe/340abc80346eeb3992337748c6a34578 to your computer and use it in GitHub Desktop.
Save tcrowe/340abc80346eeb3992337748c6a34578 to your computer and use it in GitHub Desktop.
hexo ejs hierarchical categories display
<%
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>
@tcrowe
Copy link
Author

tcrowe commented Jun 20, 2019

Screen Shot 2019-06-20 at 12 10 57 PM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment