Copied to clipboard

Flag this post as spam?

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


  • Matt 358 posts 841 karma points
    Jul 05, 2024 @ 10:52
    Matt
    0

    Umbraco 13 for each loop for root

    Hello,

    I'm having a senior moment and wondering if someone can give me a hand.

    I've got the following layout.

    • News
      • Year
      • Month

    I have my news showing in each year and month which is fine, however what I want todo is in "news" have the latest 4 news showing in the "news" template

    How can I add a for each loop in "news" that will go through the years and month and fetch latest posts with doctype "newsItem"

    Thanks

  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Jul 06, 2024 @ 11:41
    Huw Reddick
    100

    Hi Matt,

    On your news page you can use something like

    var latestNews = Model.DescendantsOrSelf().OrderByDescending(o => o.Value("ALIASOFDATEFIELD")).Take(4);
    
  • Andy Boot 48 posts 190 karma points MVP
    Jul 07, 2024 @ 17:53
    Andy Boot
    0

    To add to Huw's reply, if you use Models Builder you could add .OfType<NewsArticle>() before the ordering to explicitly order by the strongly typed property (e.g. OrderByDecending(o => o.PublishedDate)).

    Edit: You may also use DescendantsOrSelf<NewsArticle>() rather than also adding OfType<T>().

  • Matt 358 posts 841 karma points
    Jul 11, 2024 @ 10:30
    Matt
    0

    Thanks that worked great.

    How could I use this to show on my home page too?

    Cheers

Please Sign in or register to post replies

Write your reply to:

Draft