自从更新到php7.2以来,分页功能已停止在使用ACF中继器构建的库上工作。我怀疑这是count()函数的问题
我找到了一些与此相关的文章,但无法解决如何更新代码以解决此问题。如果有人能帮忙,我将不胜感激。
下面是我的代码
<?php
/*
* Paginate Advanced Custom Field repeater
*/
if( get_query_var(\'page\') ) {
$page = get_query_var( \'page\' );
} else {
$page = 1;
}
// Variables
$row = 0;
$images_per_page = 15; // How many images to display on each page
$images = get_sub_field( \'main_gallery_images\' );
$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( \'main_gallery_images\' ) ) : ?>
<div id="gallery" class="main">
<?php while( have_rows( \'main_gallery_images\' ) ): 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
$image = get_sub_field( \'image\' );
$caption = get_sub_field(\'caption\');
?>
<div class="galleryItem">
<a href="javascript:void(0)" class="enlarge">
<div class="image" style="background-image: url(<?php echo $image; ?>)">
<span><?php echo $caption; ?><em>CLOSE X</em></span>
</div>
</a>
</div>
<?php endwhile; ?>
</div>
<div class="galPag">
<?php // Pagination
echo paginate_links( array(
\'base\' => get_permalink() . \'%#%\' . \'/#gallery\',
\'format\' => \'?page=%#%\',
\'current\' => $page,
\'total\' => $pages
) );
?>
</div>
<?php endif; ?>
网站位于:
http://kalmkitchendev.wpengine.com/gallery/应该做的是分页链接更新url并显示下一组图像。发生的情况是,url得到更新,但您只需要再次显示第一组图像。“下一页”链接不会更新,始终指向第2页。
如果始终显示第一组图像,无论您在哪个子页面上,这可能意味着“页面”查询变量不存在。代码顶部的条件检查其是否存在,并将$page设置为1作为回退,这可能是正在发生的情况,但我无法确定如何修复它。
我添加了以下内容来调试该问题。
global $wp_query;
var_dump($wp_query->query_vars);
打印出来。
array(65) { ["pagename"]=> string(7) "gallery" ["do_not_redirect"]=> int(1) ["error"]=> string(0) "" ["m"]=> string(0) "" ["p"]=> int(0) ["post_parent"]=> string(0) "" ["subpost"]=> string(0) "" ["subpost_id"]=> string(0) "" ["attachment"]=> string(0) "" ["attachment_id"]=> int(0) ["name"]=> string(7) "gallery" ["static"]=> string(0) "" ["page_id"]=> int(0) ["second"]=> string(0) "" ["minute"]=> string(0) "" ["hour"]=> string(0) "" ["day"]=> int(0) ["monthnum"]=> int(0) ["year"]=> int(0) ["w"]=> int(0) ["category_name"]=> string(0) "" ["tag"]=> string(0) "" ["cat"]=> string(0) "" ["tag_id"]=> string(0) "" ["author"]=> string(0) "" ["author_name"]=> string(0) "" ["feed"]=> string(0) "" ["tb"]=> string(0) "" ["paged"]=> int(0) ["meta_key"]=> string(0) "" ["meta_value"]=> string(0) "" ["preview"]=> string(0) "" ["s"]=> string(0) "" ["sentence"]=> string(0) "" ["title"]=> string(0) "" ["fields"]=> string(0) "" ["menu_order"]=> string(0) "" ["embed"]=> string(0) "" ["category__in"]=> array(0) { } ["category__not_in"]=> array(0) { } ["category__and"]=> array(0) { } ["post__in"]=> array(0) { } ["post__not_in"]=> array(0) { } ["post_name__in"]=> array(0) { } ["tag__in"]=> array(0) { } ["tag__not_in"]=> array(0) { } ["tag__and"]=> array(0) { } ["tag_slug__in"]=> array(0) { } ["tag_slug__and"]=> array(0) { } ["post_parent__in"]=> array(0) { } ["post_parent__not_in"]=> array(0) { } ["author__in"]=> array(0) { } ["author__not_in"]=> array(0) { } ["ignore_sticky_posts"]=> bool(false) ["suppress_filters"]=> bool(false) ["cache_results"]=> bool(true) ["update_post_term_cache"]=> bool(true) ["lazy_load_term_meta"]=> bool(true) ["update_post_meta_cache"]=> bool(true) ["post_type"]=> string(0) "" ["posts_per_page"]=> int(10) ["nopaging"]=> bool(false) ["comments_per_page"]=> string(2) "50" ["no_found_rows"]=> bool(false) ["order"]=> string(4) "DESC" }
如果有人能帮忙,我会很高兴的。
最合适的回答,由SO网友:SurreyCreative 整理而成
事实证明,对$page变量的设置方式和分页链接的写入方式只做了一点小小的更改。
page的变量需要更新为paged和pagination数组中的
页码/需要前缀为%\\%
更正了下面的示例。
希望这对将来有同样问题的人有所帮助。
if( get_query_var(\'paged\') ) {
$page = get_query_var( \'paged\' );
}
<?php // Pagination
echo paginate_links( array(
\'base\' => get_permalink() . \'page/%#%\' . \'/#gallery\',
\'format\' => \'?page=%#%\',
\'current\' => $page,
\'total\' => $pages
) );
?>