gpt4 book ai didi

octobercms - 如何在 octobercms 中过滤具有多个类别的博客文章?

转载 作者:行者123 更新时间:2023-12-03 12:42:13 24 4
gpt4 key购买 nike

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

最佳答案

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

我在下面所做的是修改 PostList 组件,首先将其 fork 并将 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;
    }

关于octobercms - 如何在 octobercms 中过滤具有多个类别的博客文章?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49952555/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com