我正在尝试使用Timber的分页功能,但是呈现的链接只是将我发送到404页面。这是我获取帖子列表的方式:
layout-sw720dp
以及我如何渲染它们:
$posts = new Timber\PostQuery([
'post_type' => 'event',
'paged' => $paged,
'posts_per_page' => 6,
]);
$context['posts'] = $posts;
当我访问页面时,URL路径为{% if posts.pagination.pages is not empty %}
<nav class="navigation" role="navigation">
<ol class="pagination">
{% if posts.pagination.prev %}
{% include "wp/pagination/_pagination-link.twig" with {"class": posts.pagination.prev.class, "link": posts.pagination.prev.link, "title": "←"} %}
{% endif %}
{% for page in posts.pagination.pages %}
{% include "wp/pagination/_pagination-link.twig" with {"class": page.class, "link": page.link, "title": page.title} %}
{% endfor %}
{% if posts.pagination.next %}
{% include "wp/pagination/_pagination-link.twig" with {"class": posts.pagination.prev.class, "link": posts.pagination.next.link, "title": "→"} %}
{% endif %}
</ol><!-- /.pagination -->
</nav><!-- /.navigation -->
{% endif %}
,当我单击第二/下一页按钮时,它链接到/event/
,但是Wordpress不会运行相同的php文件({ {1}}。
如何配置分页链接?我是否可以切换它们以将页面放入查询中,例如/event/page/2/
?
答案 0 :(得分:0)
问题最终与重写规则有关。 WordPress期望的页面长度为10
,而不是6
。将其添加到functions.php
可以解决此问题:
function get_events($query)
{
if(!is_admin() && $query->is_main_query() && is_post_type_archive('event')) {
$query->set('posts_per_page', '6');
}
}
add_action('pre_get_posts', 'get_events');