October CMS如何过滤多个类别的博文?我正在使用 RainLab 博客插件。该插件仅允许过滤一种类别。我想要一个综合结果。请帮忙。
您将需要使用一些自定义代码来根据多个类别进行过滤。如果仍然使用 PostList 组件,请进行以下更改,您将能够根据多个类别过滤列表。
我下面所做的是修改 PostList 组件,首先通过分叉它并替换posts
变量与filteredPosts
多变的。然后,要设置filteredPosts
,编写代码是为了获取所有博客文章并仅返回包含以下类别的博客文章$categories
array. 诚然,这种方法绝对不是最有效的,还可以进一步改进
- 将PostList组件添加到页面中
-
在中添加以下代码Mark Up
<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) }) }}">← 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 →</a></li>
{% endif %}
</ul>
{% endif %}
-
在中添加以下代码CodeCMS 的选项卡。要改变
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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)