WordPress ACF分页库

时间:2019-06-14 10:57:54

标签: php wordpress gallery advanced-custom-fields

我正在尝试对ACF Gallery进行分页,每页限制为8张图像。

我已经成功地每页显示8张图像,但是每当我尝试访问/ page / 2或以后的任何页面时,我都会重定向到首页(第1页)

<?php
    $gallery = get_field('gallery');
    $images = array();
    $items_per_page = 8;
    $total_items = count($gallery);
    $size = 'large';
    $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)) { ?>
        <ul> <?php
        foreach ($images as $image): ?>
            <li>
                <a href="<?php echo $image['url']; ?>">
                    <img src="<?php echo $image['sizes']['large']; ?>" alt="<?php echo $image['alt']; ?>" />
                </a>
            </li> <?php
        endforeach; ?>
        </ul> <?php
    }

    $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页或随后的任何页面,我应该更改什么?

0 个答案:

没有答案