动态填充数组中的wordrpess短代码

时间:2017-08-01 10:40:37

标签: php wordpress

我正在尝试自定义wordpress智能网格插件的输出。我的目标是将图像列表添加到一个部分并通过插件输出。图像列表必须由插件短代码包装。我做过的,唯一的问题是循环。这是我的代码

            <div class="p_details_right">
                <?php 
                  //print_r($partner_pictures); 
                  $pic_bucket = [];  //empty array to hold list of image id to be used within the wordpress default gallery shortcode
                ?>
                <?php 
                        foreach ($partner_pictures as $key => $item) { 
                        array_push($pic_bucket, $key); // populating the array which holds image id
                ?>                            
                <?php 
                } 
                        //print_r($pic_bucket);
                ?>
                <?php echo do_shortcode("
                        [smart-grid]
                            [gallery ids='$pic_bucket[0],$pic_bucket[1],$pic_bucket[2]']   // <----- PROBLEM . Currently doing it statically but I need to be able to add populate the id element of the shortcode dynamically based on the $pic_bucket array.
                        [/smart-grid]
                "); ?>
            </div>

目前通过静态添加数组元素输出库,但我需要能够根据$ pic_bucket数组动态填充短代码的id元素。试图做循环但不起作用。我遗漏了一些非常基本的东西。

Sollution

将Array转换为字符串并使用该字符分配ids变量。

            <div class="p_details_right">
                <?php 
                  //print_r($partner_pictures); 
                  $pic_bucket = [];  
                ?>
                <?php 
                        foreach ($partner_pictures as $key => $item) { 
                        array_push($pic_bucket, $key);
                ?>                            
                <?php 
                } 
                        //print_r($pic_bucket);

                        $str = implode(',',$pic_bucket);
                ?>
                <?php echo do_shortcode("
                        [smart-grid]
                            [gallery ids='$str']
                        [/smart-grid]
                "); ?>
            </div>

1 个答案:

答案 0 :(得分:0)

您可以与implode(',',$array)

一起加入ID数组
相关问题