Remember me? forgot password | register

SilverStripe Forum

10691 Posts in 2798 Topics by 1519 members

Jump to:

Site Builders

If this is your first visit, you will need to register before you can post. However, you can browse all messages below.

Community » SilverStripe Forum » Site Builders » Displaying first 5 news items from multiple years
Page: 1 go to end Reply
Author Topic: Displaying first 5 news items from multiple years 495 views
  • bruceb
    avatar
    Community Member
    16 posts
    1 SilverStripe Site

    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
    Core Development Team
    1263 posts
    3 SilverStripe Sites 2 SilverStripe Themes

    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;

  • bruceb
    avatar
    Community Member
    16 posts
    1 SilverStripe Site

    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

    495 views
go to top Reply

Currently Online:

François, patrics, Apophenian, Aster

Welcome to our latest member: patrics