首页 > 解决方案 > 高级自定义字段中继器上的分页停止工作

问题描述

不确定这是否是因为升级到 PHP 7.2 或最新的 WordPress 版本,但以下代码用于允许我为转发器值添加分页。现在似乎发生的是页面只是重新加载页面。分页链接显示为/example/2/,这用于加载页面,但现在它只是重新加载example

有任何想法吗?

<?php
/* 
 * Paginatation on Advanced Custom Fields Repeater
 */

 if ( get_query_var('paged') ) {
    $page = get_query_var('paged');
 } elseif ( get_query_var('page') ) {
    $page = get_query_var('page');
 } else {
    $page = 1;
 }

// Variables
$row              = 0;
$images_per_page  = 10; // How many images to display on each page
$images           = get_field( 'image_gallery' );
$total            = count( $images );
$pages            = ceil( $total / $images_per_page );
$min              = ( ( $page * $images_per_page ) - $images_per_page ) + 1;
$max              = ( $min + $images_per_page ) - 1;

// ACF Loop
if( have_rows( 'image_gallery' ) ) : ?>

<?php while( have_rows( 'image_gallery' ) ): the_row();

    $row++;

    // Ignore this image if $row is lower than $min
    if($row < $min) { continue; }

    // Stop loop completely if $row is higher than $max
    if($row > $max) { break; } ?>

<?php $img_obj = get_sub_field( 'image' ); ?>
    <a href="<?php echo $img_obj['sizes']['large']; ?>">
        <img src ="<?php echo $img_obj['sizes']['thumbnail']; ?>" alt= "Your ALT Tag" />
    </a>

<?php endwhile;

  // Pagination
  echo paginate_links( array(
    'base' => get_permalink() . '%#%' . '/',
    'format' => '?paged=%#%',
    'current' => $page,
    'total' => $pages
  ) );
  ?>

<?php else: ?>

    <p>No images found</p>

<?php endif; ?>

标签: wordpressadvanced-custom-fieldsacfpro

解决方案


5.5 之后有一个错误跟踪器: https ://core.trac.wordpress.org/ticket/50976#comment:7

您可以在那里查看一些解决方案。这似乎是 WordPress 核心中保留的查询变量“page”的问题,因为它通常使用 ?p 和 ?page 重定向到某个页面。

所以你可能会使用其他东西。


推荐阅读