Pull Nested Child Categories by Parent Category Key


Use this example to create dynamic parent/child category listings.  The category listings can then become clickable listings to filter content by category.. used in menus, etc...


##set the myCategoryKey variable to your parent category key name (see properties on your category and make sure it is set)
##this example will show you how to pull the children and nested children of a category

#set($myCategorykey = "yourCatKey")

#foreach($con in $categories.getChildrenCategoriesByKey("$!{myCategorykey}"))
<h2>$velocityCount. Parent Category Properties</h2>
<p><strong>Velocity Variable Parent Category Name:</strong> $!{con.categoryVelocityVarName}<br/>
	<strong>Parent Category Name:</strong> $!{con.categoryName}<br/>
	<strong>Parent Category Key:</strong> $!{con.key}

	<h4>Pull of Next Level Child Category Names</h4>
	#foreach($child in $categories.getChildrenCategoriesByKey("$!{con.key}"))
##Here in the list you could href a url map pattern that includes the category and go to a detail page that lists content by category based on the path: /someMapPattern/somecategory/ (see url map doc here

##Keep nesting foreach loops in the "Pull of Next Child Category Names" to dig deeper into your category lists    

Have you created code you'd like to share with the dotCMS community?

Share your code Become a Contributor