php - 按 SilverStripe 上的类别过滤的循环博客文章

标签 php silverstripe

我正在 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);

想法是循环新闻并以某种方式对其进行编码,使其在首页上看起来不同,然后循环照片库

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

这可能吗?

最佳答案

您想在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);

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

关于php - 按 SilverStripe 上的类别过滤的循环博客文章,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38781381/

相关文章:

php - config.yml 中的 SilverStripe 类扩展

php - 格式编号因手机或商务电话而异

PHP 导入 CSV 文件时出现奇怪的字符

php - 根据第一个下拉列表选择更改下拉列表

mysql - mysql 数据库访问被拒绝

file - SilverStripe 文件标题翻译

silverstripe - 前端的 CMS 链接未转换即 href=[sitetree_link_id=xx]

php - PHP 中的远程 MySQL 连接

javascript - 如何使用循环提取数组中的json数据?

class - 在 SilverStripe 中存储针对类的配置设置