Back

Pull Nested Child Categories by Parent Category Key

Description

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...

Code

##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}
</p>

	<h4>Pull of Next Level Child Category Names</h4>
	#foreach($child in $categories.getChildrenCategoriesByKey("$!{con.key}"))
<p>
	<ul>
		<li>$!{child.categoryName}</li>
		
##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 http://www.dotcms.com/docs/latest/URLMapSEOFriendlyURLs)

	</ul>
</p>
	#end
<hr/>
#end
##Keep nesting foreach loops in the "Pull of Next Child Category Names" to dig deeper into your category lists