【问题标题】:CMB2 file_list paginate (real pages) foreach uses serialized dataCMB2 file_list paginate (real pages) foreach 使用序列化数据
【发布时间】:2016-07-01 05:17:16
【问题描述】:

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

回答 cmets 中的问题:

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

等等。

【问题讨论】:

  • 这是单页吗
  • 因为你已经从 get_post_meta(get_the_ID(), '_cmb_gallery_images', true);因此,要么所有需要使用的图像都在一个页面中,要么您为此创建了图库帖子类型。请澄清?
  • 那么你是如何循环函数的?假设你想要这个单页,你打电话给get_post_meta(get_the_ID(), '_cmb_gallery_images', true),它只是获取该页面的帖子元数据。当您在定义变量的正下方执行print_r($files); 时,您会得到什么?
  • @dingo_d - 我得到了图片的所有网址(这是它的样子,但还有更多): Array( [956] => christinacreativedesign.dev/wp-content/uploads/2016/02/… [960] => christinacreativedesign.dev/wp-content/uploads/2016/02/… [ 958] => christinacreativedesign.dev/wp-content/uploads/2016/02/… [974] =>
  • @PieterGoosen 这是 WordPress 中的一个页面模板,它有一个 CMB2 file_list 类型字段,我可以将媒体库中的任何图像附加到该字段(并上传),然后您可以制作一个图像列表在页面上。

标签: php wordpress serialization foreach meta-boxes


【解决方案1】:

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

另外,你可以在 foreach 语句中使用数组元素的直接索引来代替 array_slice,但这并不重要

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

不管怎样,主要的问题在这里:

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

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

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 谢谢。这看起来很有希望。我收到此错误Undefined variable: metas in /...
    • 我不需要get_meta_values,我得到了它的工作,并将在这个线程中发布一个要点。如果您使用要点内容更新您的答案,那么我会将其标记为已接受。如果没有你,我将无法弄清楚,所以谢谢你!尽管我目前没有需要它的项目,但它一直困扰着我,它会派上用场。
    • 好吧,我还没准备好讨论要点,因为我想解决一些与问题无关的其他内容。这是一个jsbin,我将php卡在js字段中:jsbin.com/gabozu/js
    • 嘿克里斯蒂娜。感谢 cmets 。我只是想知道您是否有一个特定页面添加了图库图片,或者它是自定义帖子类型还是在许多页面中??
    • 它无处不在,在 CMB2 中您可以指定要使用的页面模板:$cmb = new_cmb2_box( array( 'id' =&gt; $prefix . 'cmb2_gallery', 'title' =&gt; __( 'Gallery', $GLOBALS['text-domain'] ), 'object_types' =&gt; array( 'page' ), // post type 'show_on' =&gt; array( 'key' =&gt; 'page-template', 'value' =&gt; 'cmb2-gallery-page.php' ), 'context' =&gt; 'normal', 'priority' =&gt; 'default', ) );
    猜你喜欢
    • 1970-01-01
    • 2015-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-23
    相关资源
    最近更新 更多