Copied to clipboard

Flag this post as spam?

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


  • svumo 7 posts 27 karma points
    Apr 07, 2020 @ 13:32
    svumo
    0

    Most recently created blogpost first.

    So I've seen a similar post to this one on this subform already but I got a few error messages trying the same solution so I decided to create a new post.

    I am trying to make it so when I create a new blogpost it shows that one at the top of the Blog page.

    Here is my cshtml for the macro:

         @using ContentModels = Umbraco.Web.PublishedModels;
    @using Umbraco.Web;
    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @{
        var startNodeId = Model.MacroParameters["startNodeId"] != null ? Model.MacroParameters["startNodeId"] : Model.Content.Id;
    
    
        var numberOfPosts = 3;
        if (Model.MacroParameters["numberOfPosts"] != null)
        {
            int.TryParse((string)Model.MacroParameters["numberOfPosts"], out numberOfPosts);
        }
    
    
    }
    @if (startNodeId != null)
    {
        @* Get the starting page *@
        var startNode = Umbraco.Content(startNodeId);
    
        if (startNode == null)
        {
            <div class="blogposts">
                <h1>There are no posts at this time, try again later.</h1>
            </div>
    
            return;
        }
    
        //Gets all blogposts to calculate pages
        var blogposts = startNode.Children.OrderBy(x => x.CreateDate).ToList();
        var pageCount = (int)Math.Ceiling((double)blogposts.Count / (double)numberOfPosts);
        //gets the page from the querystring and sets it to one if it is out of range
        var page = 1;
        if (!string.IsNullOrEmpty(Request.QueryString["page"]))
        {
            int.TryParse(Request.QueryString["page"], out page);
            if (page <= 0 || page > pageCount)
            {
                page = 1;
            }
        }
        //Gets the blogposts for the current page
        var pagedBlogposts = blogposts.Skip((page - 1) * numberOfPosts).Take(numberOfPosts).ToList();
    
        if (pagedBlogposts.Count > 0)
        {
            <div class="blogposts">
    
                @foreach (ContentModels.Blogpost post in pagedBlogposts)
                {
                    <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>
        }
    
        if (blogposts.Count > numberOfPosts)
        {
            <div class="pagination">
                <nav class="nav-bar nav-bar--center">
                    @if (page <= 1)
                    {
                        <span class="nav-link nav-link--black nav-link--disabled">Prev</span>
                    }
                    else
                    {
                        <a class="nav-link nav-link--black" href="@(Model.Content.Url + "?page=" + (page - 1))">Prev</a>
                    }
    
                    @for (int i = 1; i <= pageCount; i++)
                    {
                        <a class="nav-link nav-link--black @(page == i ? " nav-link--active" : null)" href="@(Model.Content.Url + "?page=" + i)">@i</a>
                    }
                    @if (page == pageCount)
                    {
                        <span class="nav-link nav-link--black nav-link--disabled">Next</span>
                    }
                    else
                    {
                        <a class="nav-link nav-link--black" href="@(Model.Content.Url + "?page=" + (page + 1))">Next</a>
                    }
    
                </nav>
            </div>
        }
    }
    

    Apologises if this is a big ask as I do not know much of C# and would appreciate this very much, thank you.

  • Marc Goodson 2157 posts 14434 karma points MVP 9x c-trib
    Apr 07, 2020 @ 17:03
    Marc Goodson
    0

    Hi Svumo

    Would changing this line:

    var blogposts = startNode.Children.OrderBy(x => x.CreateDate).ToList();
    

    to

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

    make the change you are after?

    regards

    Marc

  • 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