在使用 Silverstripe 博客模块时,如何只显示分配了类别的博客文章?

How to only show blog posts that have a category assigned to them when using the Silverstripe blog module?

我正在使用 silverstripe 博客模块:https://github.com/silverstripe/silverstripe-blog

我有这样循环播放的博文:

Blog.ss

<% loop $BlogPosts %>
            <div class="row mtb20">
                <div class="col-md-8">
                    <div class="blog-holder-image" style="background-image: url($FeaturedImage.Fill(700,340).URL);"></div>
                </div>

                    <h2>$Title</h2>

                    <div>
                        <% if $Summary %>
                            $Summary
                        <% else %>
                            <p>$Excerpt</p>
                        <% end_if %>
                    </div>
                    <div>
                        <a class="call-to-action-link" href="$Link">Read more</a>
                    </div>
                </div>
            </div>
<% end_loop %>

在博客文章的顶部,我有一个类别列表,您可以单击它转到该类别:

 <% if $Categories %>
        <% loop $Categories %>
            <a class="category-btn" href="$Link">$Title</a>
        <% end_loop %>
    <% end_if %>

但是博客文章没有被过滤掉,它仍然显示所有博客文章,而不仅仅是已选择的类别,例如 "Design"

如果我使用它附带的默认 $PaginatedList 方法,它工作正常:

<% if $PaginatedList.Exists %>
            <% loop $PaginatedList %>
                <% include PostSummary %>
            <% end_loop %>
        <% else %>
            <p><%t Blog.NoPosts 'There are no posts' %></p>
<% end_if %>

如何让它按照我的方式工作?

我猜你自己找到了解决方案...查看代码,$BlogPosts gets all child pages - unfiltered。当您处于 Blog_Controller 中的类别操作时,$PaginatedList 按类别抓取预先过滤的帖子。最简单的解决方案就是使用 $PaginatedList,这就是它应该使用的方式。

但是您可能会从控制器获取 $CurrentCategory 并循环遍历其 $BlogPosts 关系,例如

<% loop $CurrentCategory.BlogPosts %>
...
<% end_loop %>