Copied to clipboard

Flag this post as spam?

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


  • bh 291 posts 976 karma points
    Nov 13, 2017 @ 16:58
    bh
    0

    List of Article Items on Homepage

    I setup an Articles page very much like this tutorial: https://our.umbraco.org/documentation/tutorials/creating-basic-site/articles-parent-and-article-items

    It works great. I have an Articles page that lists the children Article Items exactly like I want.

    My question is, how do I pull through a list of those articles to my homepage?

    I'm running v 7.7.4 if that helps any.

    Thanks in advance!

  • Alex Skrypnyk 5908 posts 22603 karma points MVP 4x admin c-trib
    Nov 13, 2017 @ 17:50
    Alex Skrypnyk
    101

    Hi Bh

    It's almost the same code, but instead of listing child nodes you have to get Articles root node first and only after that -> iterate through child nodes.

    I would use this code:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @{
        var homePage = Umbraco.AssignedContentItem.Site(); // get home page
    
        var selection = homePage.Children.FirstOrDefault(x => x.DocumentTypeAlias.Equals("articlesMain") && x.IsVisible()).Children.Where(x => x.IsVisible()).OrderByDescending(x => x.CreateDate); 
        @* OrderBy() takes the property to sort by and optionally order desc/asc *@
    }
    
    @foreach (var item in selection)
    {
        <div class="article">
            <div class="articletitle"><a href="@item.Url">@item.Name</a></div>
            <div class="articlepreview">@Umbraco.Truncate(@item.GetPropertyValue<string>("ArticleContents"),100) <a href="@item.Url">Read More..</a></div>
        </div>
        <hr/>
    }
    

    Thanks,

    Alex

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies