在Worpdress的ACF画廊分页

时间:2017-10-27 03:56:44

标签: wordpress pagination advanced-custom-fields

这是我使用ACF Gallery字段类型创建图库的方式。我成功显示了所有图像,但我只想每页显示4张图像。我知道如何对帖子类型进行分页,但我不知道如何对ACF库进行分页。谢谢!

 public function store(Request $request, $postId) {
    $post = Post::findOrFail($postId);
    $post->addComment(
        new Comment($request->all())
    );

    return back()->with('success', 'done!');
}

3 个答案:

答案 0 :(得分:1)

Working Secondary code Use this one

带有类别的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();

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

答案 1 :(得分:1)

感谢您提供示例代码和示例链接。我终于做到了。这是代码。

<?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 :(得分:-1)

您可以尝试使用此代码进行图库分页。

<?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(); 
?>
相关问题