I have news posts within a news page within a homepage on my content structure
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
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.
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>
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With