Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php CMB2文件列表分页(实际页面)foreach使用序列化数据_Php_Wordpress_Serialization_Foreach_Meta Boxes - Fatal编程技术网

Php CMB2文件列表分页(实际页面)foreach使用序列化数据

Php CMB2文件列表分页(实际页面)foreach使用序列化数据,php,wordpress,serialization,foreach,meta-boxes,Php,Wordpress,Serialization,Foreach,Meta Boxes,我很难放弃在我的图库中使用文件列表字段,但它没有分页选项(内置)(如ACF),我的php技能也很欠缺。我可以用jQuery来做,但是我想要真正的页面 这已经接近我需要的了,它是由另一个问题拼凑而成的。它从$files(get_post_meta)中分离出5个结果并创建页面,但是所有页面上的图像都是相同的。我超出了我的大脑极限 $attachment\u id=>$attachment\u url是从媒体库中选择的单个图像 $files as $attachment_id => $attac

我很难放弃在我的图库中使用文件列表字段,但它没有分页选项(内置)(如ACF),我的php技能也很欠缺。我可以用jQuery来做,但是我想要真正的页面

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

$attachment\u id=>$attachment\u url是从媒体库中选择的单个图像

$files as $attachment_id => $attachment_url
以下是我到目前为止所做的(你可能想放弃它,转而选择更好的东西):


依此类推。

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

另外,您可以在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;
函数库_loop(){
if(获取查询变量('page')){
$page=get_query_var('page');
}
否则{
$page=1;
}
//*变数
$row=0;
$images\u per\u page=5;//图像计数
$img_size='portfolio catalog';//图像大小
$images=get_post_meta(get_ID(),“_cmb_gallery_images”,true);//cmb2字段
$total=计数($images);
$pages=ceil($total/$images\u/页);
$min=($page*$images\u/页)-$images\u/页)+1;
$max=($min+$images\u/页)-1;
echo'
    ; //*创建“页面” 如果(计数($images)>0){ foreach((数组)$images as$attachment\u id=>$attachment\u url){ $row++; //如果$row低于$min,则忽略此图像 如果($row<$min){ 继续; } //如果$row高于$max,则完全停止循环 如果($row>$max){ 打破 } //回显图像 回音“
  • ”; echo wp_get_attachment_image($attachment_id,$img_size); 回音“
  • ”; }//结束foreach //*分页 回显分页链接(数组( 'base'=>get_permalink()。%#%'./', '格式'=>'?页面=%#%', “当前”=>$page, “总计”=>$pages )); }否则{ 回显“
  • 未找到图像。
  • ”; }//endif; 回声“
”; }//结束循环;
当您从get_post_meta(get_the_ID(),“cmb_gallery_images”,true)获取图像数组时,这是否适用于单个页面;因此,要么所有需要使用的图像都在一个页面中,要么您已经为此创建了图库帖子类型。请澄清?那么你是如何循环函数的?假设您希望对单个页面使用此选项,则调用
get_post_meta(get_the_ID(),“'u cmb_gallery_images',true)
,它只获取该页面的post meta。当您执行
打印时($files)就在定义变量的下面,你得到了什么?@dingo_d-我得到了图像的所有URL(看起来像这样,但还有更多):数组([956]=>[960]=>[958]=>[974]=>@PieterGoosen这是WordPress中的一个页面模板,有一个CMB2文件列表类型字段,我可以将媒体库中的任何图像附加到该字段(并上传)然后你可以在页面上列出一个图像列表。谢谢。这看起来很有希望。我得到了这个错误
未定义的变量:metas in/…
我不需要
get\u meta\u值
,我得到了它的工作,并将在这个线程中发布一个要点。如果你用要点内容更新你的答案,那么我将把它标记为接受。我不会被接受恩,没有你也能弄明白,所以谢谢你!虽然我没有当前需要它的项目,但它一直困扰着我,它会非常方便。好的,我还没有准备好接受gist,因为我想解决一些与问题无关的其他问题。这是一个jsbin,我将php插入js字段:嘿,克里斯蒂娜。谢谢你的评论。我只想知道您是否有一个特定的页面添加了图库图像,或者是自定义的帖子类型,还是在许多页面中?它在CMB2中的任何位置,您都可以指定要使用的页面模板:
$cmb=new_CMB2_box(数组('id'=>$prefix.'CMB2_gallery','title'=>uu('gallery',$GLOBALS['text-domain')),“对象类型”=>array('page'),//post-type'show_on'=>array('key'=>'page template','value'=>'cmb2 gallery page.php'),'context'=>'normal','priority'=>'default',);
$curr_page = isset($_GET['page']) ? $_GET['page'] : 0;
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;