CMB2 file_list分页(实际页面)用于使用序列化数据


CMB2 file_list paginate (real pages) foreach uses serialized data

我很难放弃使用 CMB2 file_list我的画廊,但它没有(内置)分页的选项(如 ACF),而且我的 php 技能缺乏。我可以用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

评论中问题的回答:

这适用于名为库页面.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] =>

等等。

使用以下下面

提供的代码,每页将显示 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;

我假设所有数据实际上都在相应的变量中。但无论如何,您能否在从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)