【问题标题】:How to filter blog posts with multiple categories in octobercms?如何在 octobercms 中过滤具有多个类别的博客文章?
【发布时间】:2018-10-01 19:33:58
【问题描述】:

十月 CMS 如何过滤多个类别的博文?我正在使用 RainLab 博客插件。该插件只允许过滤一个类别。我想要一个综合结果。请帮忙。

【问题讨论】:

  • 我建议coding it manually 因为插件没有附带这个功能out of the box,所以您可以使用blog-plugin 组件作为参考并将您自己的组件添加到您的插件中可以做到这一点。 . 如果您发现任何困难,我们可以解决.. 要使此功能可用,它需要相当多的努力和代码,所以我们不能在这里发布整个内容,而是我们可以小部分帮助您,以便您得到您所期望的。刚开始,我们将帮助您完成整个过程

标签: octobercms octobercms-plugins octobercms-backend


【解决方案1】:

您将需要使用一些自定义代码来根据多个类别进行过滤。如果仍在使用 PostList 组件,请进行以下更改,您将能够根据多个类别过滤列表。

我在下面所做的是修改 PostList 组件,首先将其分叉并将posts 变量替换为filteredPosts 变量。然后,要设置filteredPosts,编写代码以获取所有博客文章并仅返回包含$categories 数组中的类别的文章。 诚然,这种方法肯定不是最有效的,还可以进一步改进

  1. 将 PostList 组件添加到页面
  2. 标记中添加以下代码

    <ul class="post-list">
        {% for post in filteredPosts %}
        <li>
            <h3><a href="{{ post.url }}">{{ post.title }}</a></h3>
    
            <p class="info">
                Posted
                {% if post.categories.count %} in {% endif %}
                {% for category in post.categories %}
                    <a href="{{ category.url }}">{{ category.name }}</a>{% if not loop.last %}, {% endif %}
                {% endfor %}
                on {{ post.published_at|date('M d, Y') }}
            </p>
    
            <p class="excerpt">{{ post.summary|raw }}</p>
        </li>
    {% else %}
        <li class="no-data">{{ noPostsMessage }}</li>
    {% endfor %}
    </ul>
    
    {% if posts.lastPage > 1 %}
        <ul class="pagination">
            {% if posts.currentPage > 1 %}
                <li><a href="{{ this.page.baseFileName|page({ (pageParam): (posts.currentPage-1) }) }}">&larr; Prev</a></li>
            {% endif %}
    
            {% for page in 1..posts.lastPage %}
                <li class="{{ posts.currentPage == page ? 'active' : null }}">
                    <a href="{{ this.page.baseFileName|page({ (pageParam): page }) }}">{{ page }}</a>
                </li>
            {% endfor %}
    
           {% if posts.lastPage > posts.currentPage %}
                <li><a href="{{ this.page.baseFileName|page({ (pageParam):(posts.currentPage+1) }) }}">Next &rarr;</a></li>
            {% endif %}
        </ul>
    {% endif %}
    
  3. 在 CMS 的 代码 选项卡中添加以下代码。改变

    use RainLab\Blog\Models\Post as BlogPost;
    
    function onStart(){
        //This is where you list the categories you want to display
        $categories = ['cat-a','cat-b'];
        $posts = [];
        foreach(BlogPost::all() as $blog){
            foreach($blog->categories as $cat){
                if(in_array($cat->slug, $categories)) {
                    array_push($posts, $blog);
                    break;
                }
            }
        }
       $this['filteredPosts'] = $posts;
    }
    

【讨论】:

    【解决方案2】:

    第一个解决方案:

    在 CMS 页面的代码选项卡中:

    use RainLab\Blog\Models\Post as BlogPost;
    
    function onStart(){    
    
           $this['filteredPosts'] = BlogPost::whereHas('categories', function($q) {
                $q->whereIn('slug', ['cat-a','cat-b']);
            })->get(); 
    
    }
    

    第二种解决方案:

    在 10 月 CMS 后端中,您无法使用类别列表配置 BlogPosts 组件的 categoryFilter 属性。但是您可以让该属性为空以包含所有类别,然后您可以将 exceptCategories 属性与您不想要的类别列表一起使用。 exceptCategories 允许以逗号分隔的类别段或变量列表,其中包含您要排除的类别列表

    [blogPosts blogPostsCatACatB]
    ...
    exceptCategories = "cat-c, cat-d, cat-e"
    

    【讨论】:

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