分页是“有效的”,但未显示用于导航分页的链接

问题描述 投票:0回答:1

我可以访问.../page/1.../page/2etc ...,并且显示了帖子,所以我知道分页在技术上是可行的,但是分页链接没有显示。

木材

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">&laquo;</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">&raquo;</span>
                <span class="sr-only">Next</span>
              </a>
            </li>
          {% endif %}
        </ul>
wordpress twig timber
1个回答
0
投票

[我看到您使用此行在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_pagenumberpostsnumberpostsarg实际上是posts_per_page的别名。您可能应该只定义posts_per_page

© www.soinside.com 2019 - 2024. All rights reserved.