Copied to clipboard

Flag this post as spam?

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


  • Doogie Talons 183 posts 318 karma points
    Sep 21, 2015 @ 14:44
    Doogie Talons
    0

    How to call Children of a Certain page type, if their children have a certain page type....

    Having trouble with this one basically I want to call a category name, only if it has descendants that have articles.

    So I stared with this.

    @foreach(var newsCat in CurrentPage.Site().Descendants("NewsCategory")) {

    @newsCat.Name

    }

    But in natural language I would only want it to return a NewsCategory if that category had a descendant with a page type NewsArticle.

    so for each Site NewsCategory where NewsCategory has Descendants named NewsArticle.

    Thanks in Advance.

    Doogie

  • Mark Bowser 273 posts 860 karma points c-trib
    Sep 21, 2015 @ 15:23
    Mark Bowser
    1

    I find this sort of thing a lot easier using the more strongly typed IPublishedContent instead of the dynamics because I get to use LINQ. I'd do something like this

    @{
        var currentPage = Model.Content;
        var root = Umbraco.TypedContentAtRoot().FirstOrDefault();
    }
    
    foreach (var newsCat in root.Descendants().Where(n => n.IsDocumentType("NewsCategory") && n.Descendants().Any(d => d.IsDocumentType("NewsArticle"))))
    {
    
    }
    

    Or maybe if you don't like the really long LINQ expression, you could do something like this:

    @{
        var currentPage = Model.Content;
        var root = Umbraco.TypedContentAtRoot().FirstOrDefault();
    }
    
    foreach (var newsCat in root.Descendants().Where(n => n.IsDocumentType("NewsCategory")))
    {
        if(!newsCat.Descendants().Any(d => d.IsDocumentType("NewsArticle"))
        {
            continue;
        }
    }
    
  • Troels Larsen 75 posts 280 karma points
    Sep 21, 2015 @ 16:09
    Troels Larsen
    1

    May i ask why u use Descendants ? Your title indicates to me that u have a strukture like this

    News Node 
     - News Category 
     -- News 
     -- News 
     - News Category 
    

    then u should not use Descendants use children instead, u will get a better preformance

    @foreach (var newsCategory in Model.Content.Children.Where(child => child.IsDocumentType("NewsCategory")))
    {
        if (newsCategory.Children.Any())
        {
            <ul>
                @foreach(var news in newsCategory.Children)
                {
                    <li>@news.Name</li>
                }
            </ul>
        }
        else
        {
            <span>no children</span>
        }
    
    }
    

    if u have some other odd strukture with date folders u have to use Descendants

  • Doogie Talons 183 posts 318 karma points
    Sep 21, 2015 @ 16:28
    Doogie Talons
    0

    Aye its more like this

    Category
     - Stuff
     - Nonsense
     - MayBe News 
     -- Article
     -- Article
     -- Article
     - More stuff
    

    .

    So I want to only call the category names if it happens to have news in it.

  • Doogie Talons 183 posts 318 karma points
    Sep 21, 2015 @ 16:51
    Doogie Talons
    0
    Ah simple solution thanks to all, I thought I could do it in one for each but this works just as well without much fuss
    
    @foreach(var newsCat in CurrentPage.Site().Descendants("newsCategory"))
        {
    
            if(newsCat.Descendants("newsArticle").Any()){
    
        <li>@newsCat.Name</li>
            }
    
        }
    

    Thanks again

    Doogie

Please Sign in or register to post replies

Write your reply to:

Draft