【问题标题】:How to display an array of multiple images?如何显示多个图像的数组?
【发布时间】:2014-07-02 01:51:48
【问题描述】:

我正在使用这个 wordpress 插件来上传多张图片。 http://wordpress.org/plugins/upload-multiple-image/

它给了我一个返回数组get_multiple_image($post_id) 的函数。但是接下来我不知道如何显示这个数组?

我想以这种格式显示所有图像。

我应该怎么做才能在$img1$img2$img3$img4 中获取图像路径。

<img src="<?php echo $img1; ?>" alt="">
<img src="<?php echo $img2; ?>" alt="">
<img src="<?php echo $img3; ?>" alt="">
<img src="<?php echo $img4; ?>" alt="">

如果我这样做 print_r(get_multiple_image($post_id)); 它会返回这个

Array ( [0] => http://localhost/arabian/wp-content/uploads/2014/05/flaconi-bag1.png [1] => http://localhost/arabian/wp-content/uploads/2014/05/hoods-products1.png [2] => http://localhost/arabian/wp-content/uploads/2014/05/product-ring1.png [3] => http://localhost/arabian/wp-content/uploads/2014/05/soud-gloves1.png )

【问题讨论】:

  • print_r(get_multiple_image($post_id)) 并显示你的输出
  • 如果你想输出 img-tags 你可以使用 foreach 循环
  • 什么是数组格式?粘贴示例数组
  • 感谢 rakesh sharma... 现在我得到了一个有问题的数组。我怎样才能将这个数组保存在 $img1, $img2, .......

标签: php arrays wordpress image plugins


【解决方案1】:

快速简单:

// Get images as array
$images = get_multiple_image($post_id);

// Loop over images and echo
foreach($images as $img) {
    echo '<img src="'.$img.'" alt="">';
}

或者,如果您想设置从循环索引派生的替代图像文本:

// Loop over images and echo
for($i = 0; $i < count($images); $i++) {
    echo '<img src="'.$images[$i].'" alt="Image #'.($i+1).'">';
}

【讨论】:

    【解决方案2】:

    试试

    $images = get_multiple_image($post_id);
    foreach($images as $img) {?>
    <img src="<?php echo $img; ?>" alt="">
    <?php }?>
    

    或者对于路径,您可以使用数组索引值

    $img1 = $images[0];
    $img2 = $images[1];
    

    等等……

    【讨论】:

      【解决方案3】:

      试试这个

      $AllImages = get_multiple_image($post_id);
      
      foreach($AllImages as $image)
      {
        echo "<img src='".$image."' alt=''>";
      } 
      

      【讨论】:

        【解决方案4】:
        <?php
        $images=array('http://localhost/arabian/wp-content/uploads/2014/05/flaconi-bag1.png','http://localhost/arabian/wp-content/uploads/2014/05/hoods-products1.png','http://localhost/arabian/wp-content/uploads/2014/05/product-ring1.png','http://localhost/arabian/wp-content/uploads/2014/05/soud-gloves1.png');
        
        print_r($images);
        foreach($images as $key){
        echo "<img src='".$key."' alt=''>";
        }
        

        输出将是,

        Array
        (
            [0] => http://localhost/arabian/wp-content/uploads/2014/05/flaconi-bag1.png
            [1] => http://localhost/arabian/wp-content/uploads/2014/05/hoods-products1.png
            [2] => http://localhost/arabian/wp-content/uploads/2014/05/product-ring1.png
            [3] => http://localhost/arabian/wp-content/uploads/2014/05/soud-gloves1.png
        ) // your array
        

        结果

        <img src='http://localhost/arabian/wp-content/uploads/2014/05/flaconi-bag1.png' alt=''>
        <img src='http://localhost/arabian/wp-content/uploads/2014/05/hoods-products1.png' alt=''>
        <img src='http://localhost/arabian/wp-content/uploads/2014/05/product-ring1.png' alt=''>
        <img src='http://localhost/arabian/wp-content/uploads/2014/05/soud-gloves1.png' alt=''>
        

        See example

        【讨论】:

          【解决方案5】:

          使用此代码。

          $post_id = get_the_ID();
          $MultiImages = get_multiple_image($post_id);
          
          foreach($MultiImages as $img)
          {
            echo "<img src='".$img."' alt=''>";
          } 
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-03-10
            • 1970-01-01
            • 2020-01-10
            • 1970-01-01
            • 1970-01-01
            • 2014-01-06
            相关资源
            最近更新 更多