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...
...It does not impact the listing order on the front of the site...
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.
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).
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...
...It does not impact the listing order on the front of the site...
The Marco code looks like this...
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!
Hi Blackhawk
I believe the line of code you need to change is the following:
if you make it
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
Thanks for that tip! Yes you are right on norm. I may have some projects where we break out of that though. Much appreciated!
is working on a reply...