Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Stefan 3 posts 73 karma points
    1 week ago
    Stefan
    0

    Showing child nodes of a child page

    Hey, wondering how I could retrive the children pages of a child page. I currently have this:

    <div class="sidebar-nav">
               <div class="sidebar-nav-current">
                   <span>@CurrentPage.Name</span> <!-- TODO: Get parent name of children, not the current page -->
               </div>
    
               <ul class="sidebar-nav-items">
                   @foreach(var child in Model.Content.Children) {
                   <li class="sidebar-nav-item"><a href="@child.Url">@child.Name</a></li>
                   }
               </ul>
           </div>
    

    It'd also be great if I would be able to retrive the parent page (not the top level, ie: Homepage, but instead the parent containing all the children)

  • Marc Goodson 846 posts 5545 karma points MVP 3x c-trib
    1 week ago
    Marc Goodson
    1

    Hi Stefan

    The parent item of the Current page would be available as:

    @Model.Content.Parent.Name

    If you want to write out the children of children, then inside your 'foreach' loop you can check to see if the current looping item has children, and if so loop through them too:

       <ul class="sidebar-nav-items">
                   @foreach(var child in Model.Content.Children) {
                    if (child.Children.Any()){
                      //we've got grandchildren!!!
                      <li class="sidebar-nav-item"><a href="@child.Url">@child.Name</a>
                                <ul>
                                 @foreach(var grandChild in child.Children) {
                                         <li><a href="@grandChild.Url">@grandChild.Name</a></li>
                                    }
                                 </ul>
                       </li>
                    }
    else {
     <li class="sidebar-nav-item"><a href="@child.Url">@child.Name</a></li>          
         }
    </ul>
    

    If you need to look at children of the grandchildren - turning into a family tree - then have a look at the sitemap tutorial here: https://our.umbraco.com/documentation/Tutorials/Creating-an-XML-Site-Map/ that has some nice examples of recursiveness.

    regards

    Marc

  • Stefan 3 posts 73 karma points
    1 week ago
    Stefan
    0

    Had to modify the snippet you showed, but it worked then :) Thanks!

    For the parent title, I get "Homepage" if I use what you sent me. Is it possible to get the parent name within that scope? Not for the entire document.

  • Marc Goodson 846 posts 5545 karma points MVP 3x c-trib
    1 week ago
    Marc Goodson
    0

    Hi Stefan

    Do you mean inside the foreach loops?

    in that case you would already have the parent for each item...

    So Model.Content

    would be the 'parent' for all the children

    and child

    would be the 'parent' of all the grand children...

    but if you mean somewhere else, whereever you have an IPublishedContetn item, you can call .Parent to get the name of the parent item...

    .. if I've understood correctly!

    regards Marc

  • Stefan 3 posts 73 karma points
    1 week ago
    Stefan
    0

    Outside the @foreach loop, so for example if you're on the About page, I'd like the title "About" to be shown even if you're on a child page or even within another child page. Right now I only know how to display either the parent of the entire document or the current page.

  • Marc Goodson 846 posts 5545 karma points MVP 3x c-trib
    1 week ago
    Marc Goodson
    0

    Hi Stefan

    Them you could look at Ancestors!!

    Essentially every page in your bravo site has a 'depth' indicating how many levels deep the page sits in the content tree and every page has a 'path' property which is a comma delimed list of all the IDs of parents and parent's parents going all the way up to the root of the site

    There are a variety of methods on an IPublishedContent item that will help you query the page's ancestors. From what you describe above the page you are after the title of will always sit one below home? I'm which case try

    Model.Content.AncestorOrSelf(2)

    As I think your about page is 2 levels deep ..

    Or if depth varies and these kinds of pages have a different doc type then:

    Model Content.AncestorOrSelf("landingPageAlias")

    Would find the first ancestor of the current page 'of that particular type'

    Regards

    Marc

Please Sign in or register to post replies

Write your reply to:

Draft