0

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

RobF
  • 3
  • 2
  • The sorting of posts happens in `startNode.Children.OrderByDescending(x => x.GetPropertyValue("PublicationDate")).ToList();` which suggests the newest posts should show first. Are you sure the `PublicaitonDate` property on the blogposts is filled with different dates? – Mark Feb 04 '19 at 10:15
  • Can u Show us how does the PublicationDate look like on the back office? why not organize by Umbraco's built in createDate?? – IbraHim M. Nada Feb 04 '19 at 11:22
  • Actually there is no property 'PublicationDate' on the doc type. I guess that was written when a friend helped me out to get the thumbnail images from my 'featuredImage' property on the Latest Blog Posts list view. But I would like to sort the blog posts by date it was published and not created. So even when it is now sorting in descending order it shows the posts by creation date. Which is not quite what I want as it might happen that a post gets created today to be published in 4 weeks time – RobF Feb 04 '19 at 21:10

1 Answers1

3

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.

Claus
  • 1,975
  • 18
  • 24
  • Thanks Claus, I did exactly that already yesterday which is perfectly working as I want it to. – RobF Feb 06 '19 at 09:35