我可以访问.../page/1
,.../page/2
,etc
...,并且显示了帖子,所以我知道分页在技术上是可行的,但是分页链接没有显示。
木材
use const Flynt\Archives\POST_TYPES;
global $paged;
if (!isset($paged) || !$paged){
$paged = 1;
}
$context = Timber::get_context();
$args = array(
'numberposts' => -1,
'post_type' => 'event',
'meta_key' => 'start_date',
'orderby' => 'meta_value',
'order' => 'ASC',
'posts_per_page' => 6,
'paged' => $paged
);
$context['events'] = new Timber\PostQuery($args);
$context['options'] = get_fields('options');
Timber::render('templates/classes-and-registration.twig', $context);
Twig
<ul class="pagination">
{% if posts.pagination.prev %}
<li class="page-item">
<a class="page-link" href="{{ posts.pagination.prev.link }}" aria-label="Previous">
<span aria-hidden="true">«</span>
<span class="sr-only">Previous</span>
</a>
</li>
{% endif %}
{% for page in posts.pagination.pages %}
<li class="page-item">
{% if page.link %}
<a class="page-link" href="{{ page.link }}">{{ page.title }}</a>
{% else %}
<span class="{{page.class}}">{{page.title}}</span>
{% endif %}
</li>
{% endfor %}
{% if posts.pagination.next %}
<li class="page-item">
<a class="page-link" href="{{ posts.pagination.next.link }}" aria-label="Next">
<span aria-hidden="true">»</span>
<span class="sr-only">Next</span>
</a>
</li>
{% endif %}
</ul>
[我看到您使用此行在PHP中准备了帖子:
$context['events'] = new Timber\PostQuery($args);
但是,在分页中,您可以访问posts
上的分页,该分页可能尚不存在。您将不得不将events
重命名为posts
,或者从events
而不是posts
访问分页。
{# … #}
{% for page in events.pagination.pages %}
<li class="page-item">
{% if page.link %}
<a class="page-link" href="{{ page.link }}">{{ page.title }}</a>
{% else %}
<span class="{{page.class}}">{{ page.title }}</span>
{% endif %}
</li>
{% endfor %}
{# … #}
此外,我看到在$args
数组中,您同时定义了posts_per_page
和numberposts
。numberposts
arg实际上是posts_per_page
的别名。您可能应该只定义posts_per_page
。