Jump to:

3215 Posts in 848 Topics by 811 members

Template Questions

SilverStripe Forums » Template Questions » How to display news sorted by date?

Moderators: martimiz, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 1618 Views
  • HarryKC
    Avatar
    Community Member
    8 Posts

    How to display news sorted by date? Link to this post

    I would like to display news sorted by date instead of the order they are in the CMS.

  • simon_w
    Avatar
    Forum Moderator
    422 Posts

    Re: How to display news sorted by date? Link to this post

    I'm assuming that you're using the News articles from the tutorial.

    One way of doing this is to add your own method to the ArticleHolder in ArticleHolder.php, something like

    function OrderedChildren() {
       return DataObject::get('ArticlePage', 'ParentID = ' . $this->ID, 'Date DESC');
    }

    Then, in your template, instead of using <% control Children %>, you'll use <% control OrderedChildren %>

  • HarryKC
    Avatar
    Community Member
    8 Posts

    Re: How to display news sorted by date? Link to this post

    Thanks! Yes this was from the tutorial.

  • drew_mi
    Avatar
    Community Member
    1 Post

    Re: How to display news sorted by date? Link to this post

    I know this topic is dated...but, the News Article tutorial is the same.

    Simon_W, can you -or anyone- explain the function you gave for sorting? I'm just trying to learn php and I'm not following the logic.

    return DataObject::get('ArticlePage', 'ParentID = ' . $this->ID, 'Date DESC')

    - I understand this gets page-type 'ArticlePage'...and, the end sorts by date descending

    ~ What is the: 'ParentID = '. $this->ID

    In addition to sorting the news, how would you display only the articles with a date field within a certain range? For example, articles that should only display for a short period of time (StartDate & EndDate), or hide those that expired yesterday (ExpireDate)...

    Thank you

    1618 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.