Copied to clipboard

Flag this post as spam?

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


  • Matt 356 posts 838 karma points
    17 days ago
    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 1864 posts 6388 karma points MVP 2x c-trib
    16 days ago
    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
    14 days ago
    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 356 posts 838 karma points
    1 week ago
    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