ACF Gallery分页在Worpdress [英] ACF Gallery Pagination in Worpdress
本文介绍了ACF Gallery分页在Worpdress的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我使用ACF Gallery字段类型创建画廊的方式.我成功显示了所有图像,但是我只想每页显示4张图像.我有一个如何对帖子类型进行分页的想法,但对如何对ACF画廊进行分页却一无所知.谢谢!
This is how I created my gallery using ACF Gallery field type. I successfully displayed all of the images, But I only want to display 4 images per page. I have an idea how to paginate a post type but I have no idea on how to paginate ACF gallery. Thanks!
<?php
$images = get_field('field_59f2a1869ef2b');
$size = 'full';
if( $images ): ?>
<?php foreach( $images as $image ): ?>
<?php echo wp_get_attachment_image( $image['ID'], $size ); ?>
<?php endforeach; ?>
<?php endif; ?>
推荐答案
感谢您提供示例代码和示例链接.我终于做到了.这是代码.
Thank you for the sample codes and sample links. I finally did it. Here's the code.
<?php
$gallery = get_field('field_59f2a1869ef2b');
$images = array();
$items_per_page = 4;
$total_items = count($gallery);
$size = 'full';
$total_pages = ceil($total_items / $items_per_page);
if(get_query_var('paged')){
$current_page = get_query_var('paged');
}
elseif (get_query_var('page')) {
$current_page = get_query_var('page');
}
else{
$current_page = 1;
}
$starting_point = (($current_page-1)*$items_per_page);
if($gallery){
$images = array_slice($gallery,$starting_point,$items_per_page);
}
if(!empty($images)){
foreach( $images as $image ):
echo wp_get_attachment_image( $image['ID'], $size );
endforeach;
}
$big = 999999999;
echo paginate_links(array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => $current_page,
'total' => $total_pages,
'before_page_number' => '<span class="screen-reader-text">'.$translated.' </span>'
));
?>
这篇关于ACF Gallery分页在Worpdress的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文