Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Displaying first 5 news items from multiple years

Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, biapar, Willr, Ingo, simon_w

Page: 1
Go to End
Author Topic: 1359 Views
  • Bruce B
    Avatar
    Community Member
    145 Posts

    Displaying first 5 news items from multiple years Link to this post

    The Tutorial has an example that shows the last 5 news items on the front page. However I have grouped my media releases under ArticleHolder pages by year so I can't just use "ParentID = $this->ID" as the filter in the DataObject::get.

    I need to say: "Find all the ArticlePages that have the 'Media releases' page at Level(2)".

    Does someone have the appropriate syntax for that? Or do I need to provide more context?

    cheers
    bruce

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Displaying first 5 news items from multiple years Link to this post

    So you have 1 article holder for each year?? Then you can get a DataObjectSet of every ArticleHolder then query on each to return the children - or pages with that parent ID as we need to limit it to 5. Remember this is cut down to make it easier to read. I would add an if round the foreach to prevent any errors and you might want to get the ArticleHolders in some order.

    $output = new DataObjectSet();
    $years = DataObject::get("ArticleHolder");
    foreach($years as $year) {
    $articles = DataObject::get("ArticlePage","ParentID = $year->ID","","", 5);
    $output->push($articles);
    }
    return $output;

  • Bruce B
    Avatar
    Community Member
    145 Posts

    Re: Displaying first 5 news items from multiple years Link to this post

    Looking at that code, it would seem to pull 5 news items from each year, rather than 5 in total. I have attacked it from a different point, creating a new page type called NewsPage, which is just a duplicate of ArticlePage. Now I can pull the most recent media releases, where ever they are, using:
    function LimitedNews($limit) {
    $littlechildren = DataObject::get("NewsPage", "", "Date DESC", null, $limit);
    return $littlechildren;
    }

    cheers
    bruce

    1359 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.