Pulling Categories

You can use Velocity code to pull lists of Categories in several ways:

Pulling Top-Level Categories for a Category Field#


Consider a News Content Type with a Category field named "News Type" and Velocity variable name of "newsType":

The following Velocity code displays a list of news items, and gives information about each of the Categories assigned to the news item:

#foreach($news in $dotcontent.pull( "+contentType:News", 10, "modDate desc" )) <h2>$velocityCount. $!{news.title}</h2> <ul> #foreach($cat in $news.topic) <li><b>Category Name:</b> $!{cat.categoryName}</li> <ul> <li><i>Category Key:</i> $!{cat.key}</li> <li><i>Velocity Variable Name:</i> $!{cat.categoryVelocityVarName}</li> </ul> #end </ul> #end

Pulling Child Categories#


The following Velocity code pulls all child categories two levels deep, given a top-level Category key. Note that child categories are pulled from the Category inode, since the Category key is optional (and thus may not exist).

#set($categorykey = "topic") #set($topCategory = $categories.getCategoryByKey( $!{categorykey} )) <h2>Category: $!{topCategory.categoryName}</h2> <ul> <li><b>Key:</b> $!{categorykey}</li> <li><b>Velocity Var:</b> $!{topCategory.categoryVelocityVarName}</li> <li><b>Children:</b></li> <ul> #foreach($cat in $categories.getChildrenCategoriesByKey("$!{categorykey}")) <li>$velocityCount. $!{cat.categoryName}</h2> <ul> <li><b>Key:</b> $!{cat.key}</li> <li><b>Velocity Var:</b> $!{cat.categoryVelocityVarName}</li> <li><b>Children:</b></li> <ul> #foreach($child in $categories.getChildrenCategories("$!{cat.inode}")) <li>$!{child.categoryName} ($!{child.categoryKey})</li> #end </ul> </ul> #end </ul> </ul>