如何仅在使用Silverstripe博客模块时显示分配了类别的博客帖子?

时间:2017-07-21 03:02:19

标签: silverstripe

我正在使用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 %>

然而,博客帖子没有被过滤掉,它仍然显示所有博客帖子,而不仅仅是已经选择的类别,例如&#34; Design&#34;

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

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

我如何让它以我的方式工作?

1 个答案:

答案 0 :(得分:1)

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

但是你可能从控制器获得$CurrentCategory并循环其$BlogPosts关系,如

<% loop $CurrentCategory.BlogPosts %>
...
<% end_loop %>
相关问题