Copied to clipboard

Flag this post as spam?

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


  • blackhawk 313 posts 1368 karma points
    Aug 17, 2017 @ 18:33
    blackhawk
    0

    Sorting blogs displayed in Macro for Umbraco 7.6.5

    For the starter kit that comes with Umbraco 7.6.5 is there a way to sort the blog postings based on how we sort them in BackOffice?

    For visual purposes, this is what I mean....If I sort my blogs and publish based on the image below...

    enter image description here

    ...It does not impact the listing order on the front of the site...

    enter image description here

    The Marco code looks like this...

    @using ContentModels = Umbraco.Web.PublishedContentModels;
    @using Umbraco.Web;
    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @{ 
        var startNodeId = Model.MacroParameters["startNodeId"] != null ? Model.MacroParameters["startNodeId"] : Model.Content.Id;
        var numberOfPosts = Model.MacroParameters["numberOfPosts"] != null ? Model.MacroParameters["numberOfPosts"] : 3;
     }
    @if (startNodeId != null)
    {
        @* Get the starting page *@
        var startNode = Umbraco.TypedContent(startNodeId);
        var blogposts = startNode.Children.OrderByDescending(x => x.CreateDate).Take(3);
    
        if (blogposts.Any())
        {
            <div class="blogposts">
    
                @foreach (ContentModels.Blogpost post in blogposts)
                {
                    <a href="@post.Url" class="blogpost">
                        <div class="blogpost-meta">
                            <small class="blogpost-date">@post.CreateDate.ToShortDateString()</small>
                            <small class="blogpost-cat">
                                @Html.Partial("~/Views/Partials/CategoryLinks.cshtml", post.Categories)
                            </small>
                        </div>
                        <h3 class="blogpost-title">@post.PageTitle</h3>
                        <div class="blogpost-excerpt">@post.Excerpt</div>
                    </a>
                }
            </div>
        }
    }
    

    What must me done to implement the sorting correctly to the front of the site? I just want the sorting to be consistent even if it requires an extra step.

    Thanks!

  • Nigel Wilson 945 posts 2077 karma points
    Aug 17, 2017 @ 19:19
    Nigel Wilson
    100

    Hi Blackhawk

    I believe the line of code you need to change is the following:

    var blogposts = startNode.Children.OrderByDescending(x => x.CreateDate).Take(3);
    

    if you make it

    var blogposts = startNode.Children.Take(3);
    

    it should output the nodes in the same order as you have them sorted in the admin area.

    As per the above it is normal to sort by create date to get the latest to the top of the list, but if you want it the same as the admin area then this should "sort" it for you (bad pun - sorry).

    Cheers

    Nigel

  • blackhawk 313 posts 1368 karma points
    Aug 17, 2017 @ 19:30
    blackhawk
    0

    Thanks for that tip! Yes you are right on norm. I may have some projects where we break out of that though. Much appreciated!

Please Sign in or register to post replies

Write your reply to:

Draft