Copied to clipboard

Flag this post as spam?

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


  • loic ponchon 36 posts 56 karma points
    Sep 14, 2011 @ 17:01
    loic ponchon
    0

    Get NodeByName and browse it

    Hello,

    What i'm trying to do is to get my news node and take the 3 last published. the news published are child of the news node.

    How could i do that ? 

    thank you by advance

  • Sebastiaan Janssen 5045 posts 15477 karma points MVP admin hq
    Sep 14, 2011 @ 17:09
    Sebastiaan Janssen
    0

    That depends on how your site structure is set up, I am assuming it's a bit like this:

    Content
       - mysite.com
         - Home
         - TextPage
         - News
             - News Item 1 
             - News Item 2
             - News Item 3
             - News Item 4
             - News Item 5
             - News Item 6 

    If your document type for the News node has the alias "NewsOverview", then the code to get the latest 3 news items (from anywhere in your site) would be something like this:

    @{
      var root = Model.AncestorOrSelf(1);
      var newsRoot = root.Children.Where("NodeTypeAlias == \"NewsOverview\"").FirstOrDefault();
    
      if (newsRoot.Children.Count() > 0)
      {
        foreach (var newsItem in newsRoot.Children.OrderBy("CreateDate desc").Take(3))
        {
          <div class="newsitem">
            <div class="date">@newsItem.CreateDate</div><br />
            <a href="@newsItem.Url">@newsItem.Name</a>
          </div>
        }
      }
     }
  • loic ponchon 36 posts 56 karma points
    Sep 14, 2011 @ 17:11
    loic ponchon
    0

    Yes you rox :)

Please Sign in or register to post replies

Write your reply to:

Draft