博客分页无效。
我有2个测试帖子。我将分页设置为每页1个帖子。
显示第一页上的所有帖子,并创建分页按钮。
点击分页按钮会更改网址,但帖子和网页会保持不变。
HTML
{# Blog Post List #}
<ul class="post-list">
{% for post in filteredPosts %}
<li>
<h3><a href="/blog/{{ post.slug }}">{{ 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>
{# Pagination #}
{% 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 %}
PHP
类别过滤器
use RainLab\Blog\Models\Post as BlogPost;
function onStart(){
//This is where you list the categories you want to display
$categories = ['blog'];
$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;
}
答案 0 :(得分:2)
您的自定义数据posts
似乎取代filteredPosts
,并且您的自定义数据不符合分页
{% set posts = blogPosts.posts %}
{% for post in posts %}
这是一个循环,它遍历所有帖子,正常情况下看起来像这样
在你的情况下
{% for post in filteredPosts %}
您看到posts
来自plugin component itself
,但您overriding it
的数据为filteredPosts
function onStart(){
//This is where you list the categories you want to display
$categories = ['blog'];
$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;
}
并且我没有看到任何pagination logic in your code
所以它只显示您在filteredPosts
中的帖子,因为它使用posts
,因为分页正在构建完美由组件传递。
您的过滤逻辑似乎正在产生问题我们可以use built-in filter logic to avoid this.
从
onStart
删除您的逻辑代码并使用slug/blog/:page?/:slug?
配置
组件默认HTML /将其重置为默认值只需重新添加组件并使用它或复制此代码并使用它
{% component 'blogPosts' %}
现在,如果我们检查网址https://october-plaza.com/blog/:page?/:slug
food
food
如果您遇到任何问题,请发表评论