CMB2 file_list paginate(实际页面)foreach使用序列化数据

时间:2016-03-15 16:24:10

标签: php wordpress serialization foreach meta-boxes

我很难放弃使用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

回答评论中的问题:

这是一个名为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] =>

等等。

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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

另外,您可以在foreach语句中使用array_slice的直接索引代替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()&amp ;;来检查$ _GET [&#39;页面&#39;]围绕它(以防万一:D)