Copied to clipboard

Flag this post as spam?

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


  • Kate 267 posts 610 karma points
    Mar 25, 2020 @ 10:37
    Kate
    0

    Show a list of the 5 latest post in a blog

    Hi

    I'm trying to make a list of the 5 most recent blog posts, but can't make it work. Hope you can help :-)

    @foreach (var item in @Model.Children().Where("nodeTypeAlias == \"Blog\"")){
                    <div>Show: @item.postHeader
                    </div>
                } 
    

    My structur under "Content" is this:

    • BlogHolder (the above code works fine on this page. It shows all the blog Item
      • Blog (This is the blog item)
      • Blog (This is the blog item)
      • Blog (This is the blog item)
      • Blog (This is the blog item)

    The above code does not work on the "Blog" page. Can you guys help me with that? And I would like if it only showed the top 5 latest posts ;-)

  • Yakov Lebski 553 posts 2117 karma points
    Mar 25, 2020 @ 20:01
    Yakov Lebski
    0

    something like that

    Model.Children().Where(x=>x.ContentType.Alias== "blog").OrderBy(x=>x.CreateDate).Take(5)
    
  • Kate 267 posts 610 karma points
    Mar 25, 2020 @ 20:27
    Kate
    0

    Hi Yakov Thanks for your reply, but it didn't work.

    This is my code now

    @foreach(var item in Model.Children().Where(x=>x.ContentType.Alias== "blog").OrderBy(x=>x.CreateDate).Take(5)){
    

    and it gives me this error: "Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type"

    Does that meas anything for you. I haven't worked with Umbraco for a long time, so I apologize for my stupid questions :-)

  • Kate 267 posts 610 karma points
    Mar 25, 2020 @ 21:06
    Kate
    0

    Hi again

    I made it work this way, but don't know if it's good code. What is your opinion?

    @foreach(var item in Model.AncestorOrSelf(3).Children.Where("NodeTypeAlias == \"Blog\"").Take(5)){
    
  • Yakov Lebski 553 posts 2117 karma points
    Mar 26, 2020 @ 01:54
    Yakov Lebski
    100

    This is good, but it take 5 ordered by sort field. if you need the latest blog I guess newest, you should orderby date

    Children.Where("NodeTypeAlias == \"Blog\"").OrderBy(x=>x.CreateDate).Take(5)
    

    What version of it is Umbraco 7 or 8?

  • Kate 267 posts 610 karma points
    Mar 26, 2020 @ 17:47
    Kate
    0

    Yes, you are right and it works perfectly. Thanks for your help :-)

Please Sign in or register to post replies

Write your reply to:

Draft