【问题标题】:How to only show blog posts that have a category assigned to them when using the Silverstripe blog module?使用 Silverstripe 博客模块时,如何仅显示分配了类别的博客文章?
【发布时间】:2017-07-21 03:02:18
【问题描述】:

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

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

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

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

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

【问题讨论】:

    标签: silverstripe


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多