Wordpress Worpdress中的ACF图库分页
这是我如何使用ACF gallery字段类型创建我的库的。我成功地显示了所有图像,但我只希望每页显示4个图像。我有一个想法如何分页后类型,但我不知道如何分页ACF画廊。谢谢Wordpress Worpdress中的ACF图库分页,wordpress,pagination,advanced-custom-fields,Wordpress,Pagination,Advanced Custom Fields,这是我如何使用ACF gallery字段类型创建我的库的。我成功地显示了所有图像,但我只希望每页显示4个图像。我有一个想法如何分页后类型,但我不知道如何分页ACF画廊。谢谢 <?php $images = get_field('field_59f2a1869ef2b'); $size = 'full'; if( $images ): ?> <?php foreach( $images as $image ): ?>
<?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; ?>
您可以尝试将此代码用于图库分页。
<?php $paged = (get_query_var('page')) ? get_query_var('page') : 1;
$MyContent = " " ;//variable for <!--nextpage-->
$count =1; ?>
然后循环
<?php while(has_sub_field("content")): ?>
<?php if(get_row_layout() == "break"): ?>
<?php $MyContent .= "<!--nextpage-->\n";
$count++; ?>
<?php endif; ?>
<?php if(get_row_layout() == "section_content"): ?>
<?php if ($count != $paged){
continue; //ignore the current loop’s layout.
}?>
//your content
<?php endif; ?>
<?php endwhile; ?>
///after the loop put pagination
<?php global $post;
$post->post_content = $MyContent;
setup_postdata($post);
wp_link_pages();
?>
//你的内容
///循环结束后,放入分页
ACF分页库,包含类别
检查此代码并管理您的代码
<?php /*
Template Name: Gallery
Gallery template based on Advanced Custom Fields gallery field
with pagination and categories.
ACF Fields for this page are:
- gallery (Gallery field) -> contain all images
- categories (Repeater field) -> images divided into categories
-- name
-- nice_name (for the url)
-- gallery
*/
get_header();
//Get current cat, if empty show all images
$current_cat = get_query_var('category_name');
if (have_posts()) {
while (have_posts()) {
the_post();
echo '<div class="gallery-wrapper">';
//Get categories
$cats = get_field('categories');
//if this is gallery category subpage find out which one
if ($current_cat) {
foreach ($cats as $k=>$c) {
if ($c['nice_name']==$current_cat) {
$current_gallery = $c['gallery'];
}
}
}else {
//otherwise show whole gallery
$current_gallery = get_field('gallery');
}
//Setup pagination variables
$images = array(); // Set images array for current page
$items_per_page = 4; // How many items we should display on each page
$total_items = count($current_gallery); // How many items we have in total
$total_pages = ceil($total_items / $items_per_page); // How many pages we have in total
//Get current 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); // Get starting point for current page
// Get elements for current page
if ($current_gallery) {
$images = array_slice($current_gallery, $starting_point, $items_per_page);
}
//Show category list
if ($cats) {
echo '<ul>';
echo '<li><a href="'.get_permalink().'">All</a></li>';
foreach ($cats as $c) {
echo '<li><a href="'.get_permalink().$c['nice_name'].'/">'.$c['name'].'</a></li>';
}
echo '</ul>';
}
echo '<div id="gallery-items">';
$i = $starting_point;
if ($images) {
foreach ($images as $g) {
echo '<img src="'.$g['sizes']['thumb'].'" alt="'.$g['alt'].'">';
}
}
echo '</div>';
$big = 999999999; // need an unlikely integer
$translated = __( 'Page', 'pixplus' ); // Supply translatable string
$pagination = 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>'
));
if ($pagination) echo '<div class="pagination-wrapper">'.$pagination.'</div>';
echo '</div>';
}
}
get_footer();
感谢您提供的示例代码和示例链接。我终于做到了。这是密码
<?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>'
));
?>
回顾一下这个:如果不起作用,那么就用ajax创建自定义代码来获取限制值。哦,这救了我。谢谢!谢谢,这对我有帮助!代码不错。我使用以下内容进行了Ajax分页:。
<?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>'
));
?>