Search code examples
c#loopsforeachumbracochildren

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


Solution

  • 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>
    
        }
        }