AD5XJ
AD5XJ

Reputation: 31

How do I display recent News on silverstripe frontpage

I am using the silverstripe/simple_news module from Arno Poot for my SilverStripe 3.5.3 CMS.

The news module works perfectly and news is saved and displayed normally. I am just not happy with the way it displays on the page.

I would like to put a news brief div on the front page with the last 10 news articles. But I have little documentation to go by. How would I get the last 10 news articles.

I also would like to create a sidebar on the news holder page listing the entire news archive in order / group links by:

--------------- 
| Year        |      Page Content listing of this month only  
|   Month     | 
|     Date    |  
| Year        | 
|  Month      |  
|    Date     | 
--------------

...etc. (pub date as dd-MM-yyyy)

I think the operation should be similar but since I am somewhat new at customizing SilverStripe, I have many questions yet.

Upvotes: 1

Views: 416

Answers (1)

UncleCheese
UncleCheese

Reputation: 1584

Great question. Syndication of content is a really fundamental pattern in SilverStripe development, or really any content management system. A guiding principle of SilverStripe is that it would much rather empower you create exactly what you want by writing some code than give you 80% of what you want in some turnkey, out-of-the-box solution.

There's a lot written about this already. See https://docs.silverstripe.org/en/3/tutorials/extending_a_basic_site/#showing-the-latest-news-on-the-homepage.

What you'll want to do is create a method in your home page controller that returns a list of articles to your home page template.

public function RecentNews()
{
  return NewsArticle::get()->limit(5); // sort is already handled by default_sort
}

Then on the template:

<% loop $RecentNews %>
$Title / $Date / etc..
<% end_loop %>

Your second question about grouping them by year is a bit trickier. I did a tutorial on this a while back. Check out https://www.silverstripe.org/learn/lessons/beyond-the-orm-building-custom-sql?ref=hub

Needless to say, update all of those class names and field names to reflect the module you're using, e.g. ArticlePage -> NewsPage.

Upvotes: 5

Related Questions