Limit navigation nesting

Hi XWiki’ers,

I’m new to XWiki and I’m still learning about all it’s features, but I need help with a navigation feature. Our company has a wiki with multiple nested pages. For example:

  • PageA
    • PageA1
      • PageA2
        • PageA3
          • PageA4

I’m trying to limit the nesting of the navigation tree so that the tree only expands until PageA2, and PageA3 & PageA4 is hidden (Accessible only from PageA2 via links).

I tried adding the following code in the navigation panel’s source code:

#set ($allSpacesQuery = $services.query.hql("
    select distinct doc.space from XWikiDocument doc
    where doc.hidden = false
"))
#set ($allSpaces = $allSpacesQuery.execute())
#set ($displayedPages = [])
## Loop through all spaces and add only top-level ones
#foreach ($space in $allSpaces)
  #if (!$exclusions.contains("space:$services.model.escape($space, 'SPACE')") && !$space.contains(".") && $space != "Main")
    #set ($discard = $displayedPages.add($space))
  #end
#end
#foreach ($parent in $displayedPages)
    #set($childrenPages1 = $xwiki.getDocument($parent + ".WebHome").getChildren())
    #foreach($childPage1 in $childrenPages1)
       #set($childrenPages2 = $xwiki.getDocument($childPage1).getChildren())
       #foreach($childPage2 in $childrenPages2)
         #set($childrenPages3 = $xwiki.getDocument($childPage2).getChildren())
         #foreach($childPage3 in $childrenPages3)
            #set($pageToHide = $xwiki.getDocument($childPage3))
            #set($discard = $exclusions.add("document:" + $childPage3))
         #end
       #end
    #end
#end
#set ($exclusions = $stringtool.join($exclusions, ','))

{{documentTree showTranslations="false" showAttachments="false" compact="true" openTo="document:$openToDoc"
  exclusions="$exclusions" /}}
#panelfooter()
{{/velocity}}

It works…for some nested pages only. I can’t figure out why some pages are not hiding. They are all normal documents and not nested applications. I thought it might be symbols in the page names such as “&”, but other pages with no special characters are also not working.

Thanks in advance. If you’d like more details or resources let me know