CMB2 file_list paginate (real pages) foreach 使用序列化数据

CMB2 file_list paginate (real pages) foreach uses serialized data

我很难放弃使用 CMB2 file_list 字段我的画廊,但它没有选项(内置)分页(如 ACF)和我的 [=45= 】 技能欠缺。我可以用 jQuery 做到这一点,但是 我想要真实的页面

这已经接近我的需要了,它是从另一个问题中拼凑出来的。它从 $files (get_post_meta) 中分离出 5 个结果并创建页面,但所有页面上的图像都是相同的图像。我超出了我的大脑极限。

$attachment_id => $attachment_url 是从媒体库中选择的单张图片。

$files as $attachment_id => $attachment_url

这是我目前所拥有的(您可能想放弃它而选择更好的东西):

function gallery_loop() {

if( get_query_var('page') ) {
    $page = get_query_var( 'page' );
} else {
    $page = 1;
}


$img_size = 'portfolio-catalog';


$files = get_post_meta(get_the_ID(), '_cmb_gallery_images', true);
$limit = 5;
$total = count( $files );
$pages = ceil( $total / $limit );

$curr_page = isset($_GET['page']);
$offset = ($curr_page - 1) * $limit;


$items_array = array_chunk((array) $files, $limit, true); 

$files_array = array_slice($items_array, $offset, true); // this is showing the same 5 items on all the pages

foreach ($files_array as $files) {

    echo '<div style="border:1px solid red;">'; //BEGIN FAKE "page" so I can see if they are splitting correctly

    foreach ($files as $attachment_id => $attachment_url) {

    $page=1;

        echo '<div class="file-list-image">';
        echo wp_get_attachment_image($attachment_id, $img_size);
        echo '</div>';

    $page++;


    } // end $files as $attachment_id => $attachment_url

    echo '</div>'; //END "page" so I can see if they are splitting correctly


} // end foreach $files_array as $files



//the correct amount of pages are showing up but the items are all the same
 echo paginate_links( array(
    'base' => get_permalink() . '%#%' . '/',
    'format' => '?page=%#%',
    'current' => $page,
    'total' => $pages
  ) );


}
// end function

回答评论中的问题:

这是一个名为 gallery-page.php 的页面模板。该页面有一个名为 file_list 的 CMB2 字段类型,这是一个附加图像的地方(它们不是附加到页面,而是附加到该字段,因此您可以抓取任何图像并上传到它)。

当我从 $files = get_post_meta(get_the_ID() , '_cmb_gallery_images', true); 执行 print_r 时,我得到:

Array( [956] => http://mydevserver.dev/wp-content/uploads/2016/02/bamboo-logo.jpg [960] => http://mydevserver.dev/wp-content/uploads/2016/02/tampa_guitar_logo.jpg [958] => http://mydevserver.dev/wp-content/uploads/2016/02/CNG-refueling.jpg [974] =>

等等。

我假设所有数据实际上都在您相应的变量中。但是无论如何,在你从 post_meta 设置之后,你能提供 var_dump 的 $files 吗? (以防我的快速解决方案不起作用)

此外,您可以在 foreach 语句中使用数组元素的直接索引而不是 array_slice,但这并不重要

foreach ($items_array[$curr_page - 1] as $files) {

总之,主要问题就在这里:

$curr_page = isset($_GET['page']);

isset 总是返回 true 或 false(1 或 0)。因此,尽管有页面,您总是加载第一个或第二个页面。只需将代码替换为:

$curr_page = isset($_GET['page']) ? $_GET['page'] : 0;

此外,您可以使用 is_int() 或 is_numeric() 检查您的 $_GET['page'] 并四舍五入(以防万一 :D )

使用下方提供的以下代码,每页将显示 5 张图片,分页链接将允许用户在漂亮的 URL(例如 /gallery/2/、/gallery/3)的图库页面之间导航/ 等等。

function gallery_loop() {

    if (get_query_var('page')) {
        $page = get_query_var('page');
    }
    else {
        $page = 1;
    }

    //* variables   
    $row = 0;
    $images_per_page = 5; //image count
    $img_size = 'portfolio-catalog'; //image size
    $images = get_post_meta(get_the_ID() , '_cmb_gallery_images', true); //cmb2 field
    $total = count($images);
    $pages = ceil($total / $images_per_page);
    $min = (($page * $images_per_page) - $images_per_page) + 1;
    $max = ($min + $images_per_page) - 1;


    echo '<ul class="your-class clearfix">';

     //* create the 'pages'    
    if (count($images) > 0) {

       foreach ((array) $images as $attachment_id => $attachment_url ) {


            $row++;

            // ignore this image if $row is lower than $min
            if ($row < $min) {
                continue;
            }

            // stop loop completely if $row is higher than $max
            if ($row > $max) {
                break;
            }

            //echo the images
            echo '<li>';
            echo wp_get_attachment_image($attachment_id, $img_size);
            echo '</li>';


        } //end foreach

         //* pagination
        echo paginate_links(array(
            'base' => get_permalink() . '%#%' . '/',
            'format' => '?page=%#%',
            'current' => $page,
            'total' => $pages
        ));

    } else {

        echo '<li>No images found.</li>';

    } //endif;

    echo '</ul>';


} // end gallery_loop;