Copied to clipboard

Flag this post as spam?

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


  • Henrik Vincent 122 posts 616 karma points
    Mar 27, 2019 @ 09:41
    Henrik Vincent
    0

    Getting descendants of type

    Hi guys

    Trying to list newest blogposts from all blog categories on my blogs frontpage.

    Structure is like this

    Blog

    • Blog category
      • Blog post 1
      • Blog post 2
    • Blog category 1
      • Blog post 3
      • Blog post 4

    And so on. But I'm not sure how to get all the blog posts.

    My view looks like this:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<Blog>
    @using ContentModels = Umbraco.Web.PublishedModels;
    
    @{
        <ul class="blogCats">
            @foreach (ContentModels.BlogPost b in Model.AncestorOrSelf(1).Descendants("blogPost"))
            {
            var member = Umbraco.Member(b.Author);
            var membImg = member.Value<IPublishedContent>("memberPortrait");
    
            <li class="blogPost">
                    <a href="@b.Url">
                        <div class="blogPostImg">
                            <div class="postOverlay"></div>
                            <img src="@b.BlogPostBanner.Url" alt='@b.H1Overskrift")' />
                            <p class='postLabel @b.Parent.Name.ToString().ToLower().Replace("æ", "ae").Replace("ø", "oe").Replace("å", "aa").Replace(" ", "")'>@b.Parent.Name</p>
                            <div class="authorTitleWrap">
                                <div class="postedByWrapper">
                                    <div class="postedBy">
                                        <img src='@membImg.Url' alt='@member.Name' />
                                        <p>@member.Name |<span class="mdi mdi-calendar-text"></span><span class="date">@b.CreateDate.ToString("dd/MM/yyyy")</span></p>
                                    </div>
                                </div>
                                <h2 class="postHeader">@b.H1Overskrift</h2>
                            </div>
                        </div>
                    </a>
                    <div class="postTeaser">@b.BlogPostTeaser</div>
                    <a class="blogReadMore" href="@b.Url">Læs artiklen</a>
            </li>
    
            }
        </ul>
    }
    

    But I'm getting nothing back.

    I tried removing almost everything and just get names, but still nothing.

    Hope you guys can help me

    Best

    Henrik

  • Thomsen 113 posts 336 karma points
    Mar 27, 2019 @ 09:44
    Thomsen
    100

    Try this:

    .DescendantsOfType("blogPost");
    
  • Henrik Vincent 122 posts 616 karma points
    Mar 27, 2019 @ 09:50
    Henrik Vincent
    0

    Perfect, Thomsen

    Thanks a bunch!

  • Chris Randle 85 posts 199 karma points c-trib
    Oct 24, 2019 @ 22:26
    Chris Randle
    2

    To avoid magic strings, and enforce type safety, instead of this

    .DescendantsOfType("blogPost");
    

    use the following

    .Descendants<BlogPost>();
    

    Type-safety means that if the class BlogPost is renamed, then you will get a compile-time exception rather than a runtime one. It's better that the developer finds the exception before the customer or site visitor sees it, right? A good adage is to avoid magic strings at all costs.

  • Henrik Vincent 122 posts 616 karma points
    Oct 25, 2019 @ 06:57
    Henrik Vincent
    0

    Thanks a lot Chris :)

Please Sign in or register to post replies

Write your reply to:

Draft