【问题标题】:Loop blog post filtered by a category on SilverStripe按 SilverStripe 上的类别过滤的循环博客文章
【发布时间】:2016-08-05 04:54:32
【问题描述】:

我在 SilverStripe 网站上工作并安装了blog module

我的博客设置了多个类别,例如 新闻活动公告图片库等。

我遇到的问题是我想在首页展示每个类别的最新博文(标题、图片、部分内容)。

我可以使用此解决方案轻松循环博客文章:http://www.silverstripe.org/community/forums/blog-module-forum/show/102585?start=8

/mysite/code/Page.php

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

/themes/simple/templates/Page.ss

<ol>
<% loop $latestBlog %>
    <li>$Title</li>
    <p>$Content</p>
<% end_loop %>
</ol>

但我不知道在按类别过滤时如何循环。例如像这样的逻辑:

return BlogPost::get()
        ->FILTER('Category', 'News')
        ->sort('PublishDate', 'desc')
        ->limit($num);

这个想法是循环 News 并以某种方式对其进行编码,使其在首页上看起来不同,然后循环 Photo Gallery

我找不到任何有关如何执行此操作的内容。

这样可以吗?

【问题讨论】:

    标签: php silverstripe


    【解决方案1】:

    你想在DataList上使用relation方法:http://api.silverstripe.org/3.3/class-DataList.html#_relation

    return BlogCategory::get()
        ->filter('Title', 'News')
        ->relation('BlogPosts')
        ->sort('PublishDate', 'DESC')
        ->limit($num);
    

    这将返回按定义的类别(新闻)过滤的博客文章列表。

    【讨论】:

    • 做到了。非常感谢
    猜你喜欢
    • 2017-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-23
    相关资源
    最近更新 更多