Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Umbraco foreach children of a child page

I have news posts within a news page within a homepage on my content structure

Example:

Homepage
- News
-- News Posts

I'm looking to have some of the news feed on my homepage in a foreach statement. In my head it should be as simple as:

@foreach (var homenews in CurrentPage.Children.Children)
{
     if (homenews.Name == "News Post")
     {
         //Do some stuff//
     }
}

Obviously that doesn't work so has anybody got any ideas? Thanks

like image 992
Phil Avatar asked Feb 12 '23 12:02

Phil


2 Answers

When you're walking the tree you have to remember that a property (or method) like Children or Descendants() will return a collection of objects, so you can't just call Children of a collection. You can only call Children on a single object.

You can find the correct child of the homepage by using something like var newsPage = CurrentPage.Children.Where(x => x.DocumentTypeAlias == "NewsListingPage") and then extract the children of that page.

like image 156
Digbyswift Avatar answered Feb 15 '23 11:02

Digbyswift


I ended up getting the news page by its id and then getting it's children from there. The below code worked for me. Thanks guys.

@{
    var node = Umbraco.Content(1094);    
    <p>@node.Id</p> // output is 1094  
    foreach (var item in node.Children.Where("Visible").Take(3))

    {

         <p>@item.exampleText</p>

    }
    }
like image 29
Phil Avatar answered Feb 15 '23 11:02

Phil