简体   繁体   English

Silverstripe博客-主页上特定博客的最新帖子

[英]Silverstripe Blog - Recent posts from a specific blog on homepage

I am trying to include recent blogposts from a specific blog in my Silverstripe homepage. 我正在尝试将Silverstripe主页中来自特定博客的最新博客文章包括在内。 I have this code returning posts from ALL my blog pages (there are two on the site): 我有此代码从我所有博客页面(网站上有两个)返回帖子:

public function latestBlog($num = 3) {
    return BlogPost::get()
        ->sort('PublishDate', 'desc')
        ->limit($num);
}

. Advice on how to specify the blog ID in a filter? 关于如何在过滤器中指定博客ID的建议?

Many thanks in advance. 提前谢谢了。

You probably want to filter by the correct blog to start with, then apply your sort and limit to the blog posts that belong to it, for example: 您可能想要按正确的博客开始过滤,然后将排序和限制应用于属于它的博客帖子,例如:

public function latestBlog($num = 3)
{
    return Blog::get()                               // <- DataList of Blogs
        ->filter('URLSegment', 'your-specific-blog') // <- target a specific Blog
        ->first()                                    // <- get that specific Blog
        ->Children()                                 // <- get the BlogPost children for it
        ->sort('PublishDate', 'desc')                // <- sort the BlogPosts
        ->limit($num);                               // <- limit the list
}

Note that this doesn't include any error checking - eg the ->first() call could return null so it'd be worth checking that before continuing. 请注意,这不包括任何错误检查-例如->first()调用可能返回null,因此值得在继续之前进行检查。

You could also move the URLSegment filter into a function argument, or swap it for any other property filter. 您也可以将URLSegment过滤器移到函数参数中,或将其交换为任何其他属性过滤器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM