Copied to clipboard

Flag this post as spam?

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


  • Luis Oliveira 15 posts 124 karma points
    6 days ago
    Luis Oliveira
    0

    Blogpost Order by Last published

    Hi everyone

    I have a page with some articles (blogposts) and when i publish one i would like have this post as last published and visible at first of the last ones. What happen is, every articles/blog post i published they go to the last.

    Its possible inverse this order?

    thank you Luis

  • Dan Diplo 1475 posts 5704 karma points MVP 3x c-trib
    6 days ago
    Dan Diplo
    100

    What code do you have to get them currently, as this will require modifying?

    In general terms you'll need to write a query that appends OrderByDescending for the date property on the end to reverse the order.

    For instance, this would work:

    Model.Children().OrderByDescending(p => p.CreateDate)
    

    If you have models for your posts (eg. BlogPost) then you can do:

    Model.Children<BlogPost>().OrderByDescending(p => p.CreateDate)
    

    Hope that gives you enough to get there...

  • Luis Oliveira 15 posts 124 karma points
    6 days ago
    Luis Oliveira
    0

    Hi Dan

    Thanks for your help :) Later i will check this and keep you informed if results.

    br Luis

  • Luis Oliveira 15 posts 124 karma points
    6 days ago
    Luis Oliveira
    0

    Hello Dan

    Sorry taking your time again. Your suggestion really fix my issue in my blogposts pages. But i notice the 3 blogposts i have on frontpage (index) didn't change the order.

    I have open the Home template and see if there is any code to change but not, redirect to master.chtml and there also didn't find.

    You know how i can make the last post could be visible also on frontpage?

    This is the code for Blog template:

    @inherits UmbracoViewPage

    @{ Layout = "master.cshtml";

    var blogItems = Model.Children.Select(x => new Blogpost(x)).OrderByDescending(p => p.CreateDate).ToList();;
    var hasPaging = true;
    var numberOfPosts = (int)Model.ItemsToShow;
    var pageCount = (int)Math.Ceiling((double)blogItems.Count / (double)numberOfPosts);
    var page = SetPageNumber(pageCount);
    var pagedBlogposts = blogItems.Skip((page - 1) * numberOfPosts).Take(numberOfPosts).ToList();
    

    }

    @Html.Partial("~/Views/Partials/SectionHeader.cshtml", new HeaderSection(Model))

    @Html.Partial("~/Views/Partials/Renderers/RenderSections.cshtml", Model.NestedContentBefore)

    @if (!blogItems.HasValue()) {

    There are no posts at this time, try again later.

    return; }

    @if (pagedBlogposts.Count > 0) { @SectionHelper.RenderSectionStart(Model.BackgroundColor?.Label, true)

    foreach (var blogPost in pagedBlogposts)
    {
        <div class="column @RazorHelper.ColumnClass(Model.ItemsPerRow?.ToString())">
            @Html.Partial("~/Views/Partials/Nested/Item/BlogItem.cshtml", blogPost)
        </div>
    }
    
    if (blogItems.Count > numberOfPosts && hasPaging)
    {
        <div class="column is-12">
            <div class="buttons is-centered">
                @for (int i = 1; i <= pageCount; i++)
                {
                    <a class="button is-medium @(page == i ? "is-light" : "is-link")" href="@("?page=" + i)">@i</a>
                }
            </div>
        </div>
    }
    @SectionHelper.RenderSectionEnd()
    

    } @Html.Partial("~/Views/Partials/Renderers/RenderSections.cshtml", Model.NestedContentAfter)

    @functions { //gets the page from the querystring and sets it to one if it is out of range int SetPageNumber(int pageCount) { var page = 1; if (!string.IsNullOrEmpty(Request.QueryString["page"])) { int.TryParse(Request.QueryString["page"], out page); if (page <= 0 || page > pageCount) { page = 1; } } return page; } }

    and this for Home template:

    @using Umbraco.Web.PublishedModels @inherits Umbraco.Web.Mvc.UmbracoViewPage

  • Dan Diplo 1475 posts 5704 karma points MVP 3x c-trib
    5 days ago
    Dan Diplo
    0

    Hi! I'm finding it quite tricky to read the contents. Is there definitely no specific code on the home page, maybe in a partial, for there? It sounds like you need to locate this code first.

  • Luis Oliveira 15 posts 124 karma points
    5 days ago
    Luis Oliveira
    0

    Hi Dan

    As you say i was searching on partial and i found this template, maybe its here...what you think?

    BlogListSection.cshtml

    @inherits Umbraco.Web.Mvc.UmbracoViewPage

    @{ // get the list of blogs var blogsNode = new Blog(Model.Blog); var blogs = blogsNode.Children.Select(x => new Blogpost(x)).Take((int)Model.ItemsToShow); var itemsPerRow = Model.ItemsPerRow?.ToString(); }

    @SectionHelper.RenderSectionStart(Model.BackgroundColor.Label) @Html.Partial($"~/Views/Partials/Nested/Component/TitleComponent.cshtml", new TitleComponent(Model))

    @foreach (var blogItem in blogs) { blogItem.ItemsPerRow = itemsPerRow;

    @Html.Partial($"~/Views/Partials/Nested/Item/BlogItem.cshtml", blogItem)
    } @Html.Partial($"~/Views/Partials/Nested/Component/LinkComponent.cshtml", new LinkComponent(Model)) @SectionHelper.RenderSectionEnd()

  • Dan Diplo 1475 posts 5704 karma points MVP 3x c-trib
    4 days ago
    Dan Diplo
    1

    Hi Luis,

    Could be! If so, you probably need to add an order by to the line that gets the blogs. Something like:

    var blogs = blogsNode.Children.Select(x => new Blogpost(x)).OrderByDescending(p => p.CreateDate).Take((int)Model.ItemsToShow);
    

    Does that help?

  • Luis Oliveira 15 posts 124 karma points
    3 days ago
    Luis Oliveira
    0

    Hi Dan

    Yes! Helps :) Thank you..five stars!!

    Luis

Please Sign in or register to post replies

Write your reply to:

Draft