ACF Gallery Pagination in Worpdress

霸气de小男生 提交于 2019-12-08 12:39:08

问题


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; ?> 

回答1:


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>'
));


?>



回答2:


Working Secondary code Use this one

ACF Pagination Gallery with categories

Review This one and manage your code.

<?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();

Function.php (gallery_rewrites)

add_action( 'init', 'gallery_cat_rewrite' );
function gallery_cat_rewrite() {
    //My subpage slug is "gallery" if you have something else, change gallery to that
    add_rewrite_rule(
        'gallery/([^/]+)/?$',
        'index.php?pagename=gallery&category_name=$matches[1]',
        'top' );

    add_rewrite_rule(
        'gallery/([^/]+)/page/([0-9]+)?$',
        'index.php?pagename=gallery&category_name=$matches[1]&paged=$matches[2]',
        'top' );

    // Remember to flush the rules once after modyfing something here.
    // After first page refresh comment this line so that rule wouldn't be flushed over and over again.
    flush_rewrite_rules();
}



回答3:


You can try this code for gallery pagination.

<?php  $paged = (get_query_var('page')) ? get_query_var('page') : 1; 
        $MyContent = " " ;//variable for <!--nextpage-->
        $count =1; ?>

then loop

<?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(); 
?>


来源:https://stackoverflow.com/questions/46967274/acf-gallery-pagination-in-worpdress

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!