简体   繁体   English

Umbraco预约儿童页面的儿童

[英]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. 我希望在foreach声明中在我的主页上提供一些新闻。 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. 当你走在树上时,你必须记住像ChildrenDescendants()这样的属性(或方法Descendants()将返回一个对象集合,因此你不能只调用一个集合的Children You can only call Children on a single object. 您只能在单个对象上调用Children

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. 您可以使用var newsPage = CurrentPage.Children.Where(x => x.DocumentTypeAlias == "NewsListingPage")类的内容找到主页的正确子项,然后提取该页面的子项。

I ended up getting the news page by its id and then getting it's children from there. 我最终得到了它的id的新闻页面,然后从那里得到它的孩子。 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>

    }
    }

You need to reference the required node by NodeTypeAlias of it's Document Type. 您需要通过NodeTypeAlias的文档类型引用所需的节点。

So assuming the alias of the DocType of your News Posts is “NewsPosts” then... 因此,假设您的新闻帖的DocType的别名是“NewsPosts”,那么......

@foreach (var homenews in @Model.Descendants().Where("NodeTypeAlias == \"NewsPosts\"")).Take(3)
{       
    <p>@homenews.Name<p>
}   

...should return the name of the first 3 news posts. ...应该返回前3个新闻帖的名称。

I had the exact scenario, this is how I got mine working. 我有确切的情况,这就是我的工作方式。 NodeByID was very useful nad -1 indicates the root NodeByID非常有用nad -1表示根

@foreach(var item in Model.NodeById(-1).Children)
    {
string itemName = item.Name;
switch(itemName)
{
    case "News":
        @* News *@
    <div id="News"> 
      <h3><a href="@item.Url">@item.Name</a></h3>
   @foreach (var newsPost in item.Children.OrderBy("UpdateDate desc").Take(4).Items)
        {
            <p>
            <a href="@newsPost.Url">@newsPost.Title</a>
             </p>
        }

    </div>
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM