Skip to content

Instantly share code, notes, and snippets.

@tcrowe tcrowe/example.ejs
Created Jun 20, 2019

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

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
You can’t perform that action at this time.