Copied to clipboard

Flag this post as spam?

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


  • Matt 353 posts 825 karma points
    Mar 12, 2018 @ 22:31
    Matt
    0

    Show latest blogs on home page

    Hello all,

    So I've been following this tutorial on umbraco;

    https://umbraco.tv/videos/umbraco-v7/implementor/fundamentals/creating-a-site-from-scratch/preview-of-completed-site/

    And I'm now looking to show the latest blog posts on the home page and was wondering if someone can point me in the right direction of how I could achieve this.

    This is what I'm looking to do;

    enter image description here

    Thanks in advance,

    Matt

  • Daniel Chenery 119 posts 465 karma points
    Mar 12, 2018 @ 22:46
    Daniel Chenery
    0

    Assuming the Homepage is your root node, something like the following should do it Model.Content.Descendants("blog").FirstOrDefault(), where blog is your document type alias

    If you're using models builder you can do Descendants<Blog>() instead

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Mar 13, 2018 @ 07:42
    Michaël Vanbrabandt
    100

    Hi Matt,

    is it possible to show us a preview of your content structure?

    Presume you have a structure like this:

    Home
        Blog ( document type `Blog Root` )
            Blogpost 1 ( document type `Blog Item` )
            Blogpost 2 ( document type `Blog Item` )
            Blogpost 3 ( document type `Blog Item` )
    

    Then you can do something like this:

    1. First we are going to take the Blog node

      var blogNode = UmbracoHelper.TypedContentSingleAtXPath("//blogRoot");
      

    So basically we ask Umbraco to take the first node from top which has a document type alias of blogRoot.

    1. Get latest Blog Item nodes

      var blogItems = blogNode.Children.OrderByDescending(x => x.CreateDate).Take(3);
      

    Here we ask for the children of the Blog Root node and sort them by there creation date, then at the end using Take() we can say to only return the last 3 items.

    *Code is manually written, so could contain typos.

    Hope this helps.

    /Michaël

  • Daniel Chenery 119 posts 465 karma points
    Mar 13, 2018 @ 09:17
    Daniel Chenery
    0

    I could be wrong here, it's been a while since I checked, but shouldn't it be OrderByDescending(c => c.CreatedDate) so it doesn't throw deprecation warnings for dynamic casting?

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Mar 13, 2018 @ 09:22
    Michaël Vanbrabandt
    0

    Yes you are correct, I have edit the post above!

    /Michaël

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Mar 21, 2018 @ 13:21
    Michaël Vanbrabandt
    0

    Hi Matt,

    did you solve this issue? Can you share it with the community for others that are in the same situation as you?

    Have a nice day

    /Michaël

  • Matt 353 posts 825 karma points
    Mar 21, 2018 @ 15:46
    Matt
    0

    Currently having problems with our install at the moment (not related to this)

    But once its up and working again I will post back :)

  • Matt 353 posts 825 karma points
    Mar 27, 2018 @ 10:42
    Matt
    0

    Hello,

    I created it using partial views and using the query builder.

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    
    
    
    @{
        var selection = Model.Content.Site().FirstChild("blog").Children("blogItem")
                            .Where(x => x.IsVisible()).Take(3);
    }
    
        @foreach(var item in selection){
    
                      <div class="recent-blog-div">
                        <a href="@item.Url" class = "main-page-blog-title-links"><h4 class="recent-blog-title">@item.GetPropertyValue("blogPostShortTitle")</h4></a>
                        <div class="text-block-3">@Umbraco.Field("createDate")</div>
                      </div>
        }
    

    My next question is how do I display the latest 3? currently its displaying them from 1st blog created.

  • Daniel Chenery 119 posts 465 karma points
    Mar 27, 2018 @ 10:44
    Daniel Chenery
    2

    As mentioned above, OrderByDescending(c => c.CreatedDate) is your friend.

    Try this

    var selection = Model.Content.Site().FirstChild("blog").Children("blogItem").OrderByDescending(c => c.CreateDate).Where(x => x.IsVisible()).Take(3)
    
  • Matt 353 posts 825 karma points
    Mar 27, 2018 @ 12:57
    Matt
    0

    Hmm that didnt work for me I had to use;

    .OrderBy("CreateDate desc")
    

    Which done the job, thanks :)

  • Daniel Chenery 119 posts 465 karma points
    Mar 27, 2018 @ 12:58
    Daniel Chenery
    0

    Ah, sounds like you're using the older Dynamics syntax.

    It works in V7, but is due to be removed in V8 - In fact, you should have deprecation warnings in the current versions of Umbraco I believe :)

  • Paul Seal 524 posts 2889 karma points MVP 6x c-trib
    Mar 27, 2018 @ 13:37
    Paul Seal
    2

    I would always have a datetime field for article date. That way you can create or update an article and not have it interfere with the ordering.

Please Sign in or register to post replies

Write your reply to:

Draft