【问题标题】:Blog Pagination not working博客分页不起作用
【发布时间】:2018-06-05 19:48:10
【问题描述】:

博客分页不起作用。

我有 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) }) }}">&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 %}

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;
}

【问题讨论】:

    标签: octobercms


    【解决方案1】:

    您似乎正在用您的自定义数据 filteredPosts 覆盖 posts,并且您的自定义数据不考虑分页

    {% 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' %}
    

    现在如果我们检查 URL https://october-plaza.com/blog/:page?/:slug

    如果你遇到任何问题,请发表评论

    【讨论】:

      猜你喜欢
      • 2015-12-19
      • 2014-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-02
      • 2016-12-14
      • 2018-12-29
      • 2018-05-24
      相关资源
      最近更新 更多