How do I display recent News on silverstripe frontpage

时间:2017-04-24 17:17:02

标签: php date silverstripe

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.

1 个答案:

答案 0 :(得分:5)

好问题。内容联合是SilverStripe开发中真正的基本模式,或者实际上是任何内容管理系统。 SilverStripe的一个指导原则是,它更能让您通过编写一些代码来创建您想要的内容,而不是在一些交钥匙,开箱即用的解决方案中为您提供80%的所需内容。

已经有很多关于此的文章。见https://docs.silverstripe.org/en/3/tutorials/extending_a_basic_site/#showing-the-latest-news-on-the-homepage

您要做的是在主页控制器中创建一个方法,该方法会将文章列表返回到您的主页模板。

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

然后在模板上:

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

关于按年度分组的第二个问题有点棘手。我前一段时间做了一个教程。查看https://www.silverstripe.org/learn/lessons/beyond-the-orm-building-custom-sql?ref=hub

毋庸置疑,更新所有这些类名和字段名称以反映您正在使用的模块,例如: ArticlePage - &gt; NewsPage。