Search code examples
sortingumbracoblogsumbraco7umbraco-blog

Umbraco Starter Kit sorting blog posts in wrong order


I tried to find the right answer first but had no luck.

I am working on a website using the Starter Kit of Umbraco in V7 For some reason, it sorts the blog posts using oldest first, while I want to get the newest posts first in the list view using Partial View Macro 'GetLatestBlogPosts'

Here is the code I'm currently using for the Partial View Macro:

@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 = 3;
if (Model.MacroParameters["numberOfPosts"] != null)
{
    int.TryParse((string)Model.MacroParameters["numberOfPosts"], out  

numberOfPosts);
}


}
@if (startNodeId != null)
{

 @* Get the starting page *@
var startNode = Umbraco.TypedContent(startNodeId);
//Gets all blogposts to calculate pages
var blogposts = startNode.Children.OrderByDescending(x =>  x.GetPropertyValue("PublicationDate")).ToList();
var pageCount = (int)Math.Ceiling((double)blogposts.Count /   (double)numberOfPosts);

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.ToLongDateString()</small>
                    <small class="blogpost-cat">
                        @Html.Partial("~/Views/Partials/CategoryLinks.cshtml", post.Categories)
                    </small>
                </div>

                @if(post.FeaturedImage != null){
                <div class="blogpost-image">
                    <img class="img-responsive" src="@post.FeaturedImage.Url?width=200" alt="@post.PageTitle" style="width: 100%; height: 100%;"/>
                </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>
}
}

I am completely stuck. Tried some things but all I try is going to break the site. Would really appreciate some help here to get this sorted in the desired way.

Thanks a lot for some guidance and assistance


Solution

  • As you figured out yourself, you are trying to sort by a non-existing property on your item.

    I would recommend that you add a property on your blog posts called publishDate or something like that. You can then set this property on each of your posts to indicate when they are 'published'.

    The reason why you would not want to just use the build in properties (like created date or published date) for something like this, is that if you need to update an old blog post (maybe correcting a typo) the publish date will now be updated when you republish. So simply correcting a typo will now make the old blog post appear as it being the latest blog post.

    You should of course implement a fallback so if this property isn't set, the publish date will fallback to using the actual latest publish date from the item.