木材分页

时间:2017-08-30 14:22:14

标签: wordpress twig timber

我正在使用Timber,目前正试图按照Timber docs进行分页工作,尽管到目前为止还没有成功。下面是我的自定义存档php文件:

global $paged;
if (!isset($paged) || !$paged){
    $paged = 1;
}
$context = Timber::get_context();
$args = array(
    'post_type' => 'product',
    'posts_per_page' => 2,
    'paged' => $paged,
);
$context['subCatItems'] = new Timber\PostQuery($args);

Timber::render('archive-product.twig', $context);

我相应的Twig文件:

<div class="l-container vert-push--small vert-push--adult--medium">
    {% for post in posts %}
        <div class="c-std">{{ post.title }}</div>
    {% endfor %}
</div>

<div class="tool-pagination c-std">
    {% if posts.pagination.prev %}
        <a href="{{posts.pagination.prev.link}}" class="prev {{posts.pagination.prev.link|length ? '' : 'invisible'}}">Prev</a>
    {% endif %}
    <ul class="pages">
        {% for page in posts.pagination.pages %}
            <li>
                {% if page.link %}
                    <a href="{{page.link}}" class="{{page.class}}">{{page.title}}</a>
                {% else %}
                    <span class="{{page.class}}">{{page.title}}</span>
                {% endif %}
            </li>
        {% endfor %}
    </ul>
    {% if posts.pagination.next %}
        <a href="{{posts.pagination.next.link}}" class="next {{posts.pagination.next.link|length ? '' : 'invisible'}}">Next</a>
    {% endif %}
</div>

目前的主要问题是posts_per_page似乎什么都不做,我只是在页面上呈现所有帖子(目前大约20个),当时应该只有2个分页(其中目前没有显示。有什么想法吗?

1 个答案:

答案 0 :(得分:2)

不幸的是,我没有权利发表评论,所以我必须以这种方式发帖。

您的代码显示您呈现'archive-product.twig'并查询您的'产品'帖子类型,因此您应该尝试根据您链接的文档使用活动查询的方法。你准备好了吗?

相关问题