1

I'm getting list of image using foreach, but I don't want to display first image of array, but I don't know how to make it works. Here my code:

$portfolio_gallery_image = get_post_meta( get_the_ID(), "portfolio_gallery_image", true );

<div class="folio-gallery grid-masonry clearfix">
    <?php
    foreach ( $portfolio_gallery_image as $key => $image ){
    ?>
        <a class="folio-item col-3 ndSvgFill grid-item" href="<?php echo esc_url($image) ?>">
            <img src="<?php echo esc_attr($image) ?>" alt="portfolio">
        </a>
    <?php } ?>
</div>

This is array

Array
(
    [2098] => http://dione.thememove.com/wp-content/uploads/2016/04/11.jpg
    [2097] => http://dione.thememove.com/wp-content/uploads/2016/04/10.jpg
    [2062] => http://dione.thememove.com/wp-content/uploads/2016/06/f_08.jpg
    [2084] => http://dione.thememove.com/wp-content/uploads/2016/06/10.jpg
    [2096] => http://dione.thememove.com/wp-content/uploads/2016/04/9.jpg
    [2082] => http://dione.thememove.com/wp-content/uploads/2016/06/08.jpg
    [2094] => http://dione.thememove.com/wp-content/uploads/2016/04/7.jpg
)

Really appreciate your help. Thank you.

The Hung
  • 309
  • 4
  • 18

2 Answers2

2

You can do like this,

<?php

function custom_function(&$arr)
{
    list($k) = array_keys($arr);
    $r = array($k => $arr[$k]);
    unset($arr[$k]);
    return $r;
}
custom_function($portfolio_gallery_image);
foreach ($portfolio_gallery_image as $key => $image) {

    ?>
    <a class="folio-item col-3 ndSvgFill grid-item" href="<?php echo esc_url($image) ?>">
        <img src="<?php echo esc_attr($image) ?>" alt="portfolio">
    </a>
<?php } ?>

array_shift Shift an element off the beginning of array.

Rahul
  • 18,271
  • 7
  • 41
  • 60
  • Thank you. I got this error: `Warning: Invalid argument supplied for foreach()` – The Hung Apr 19 '17 at 05:07
  • if your array is like e.g. [a,b,c,d] then array_shift will skip a and then array will have [b,c,d] this is how it works. May I know how your array looks like, until n unless I wont get idea regarding manipulation of your array – Rahul Apr 19 '17 at 05:11
1

Try below code to remove first key/value from array before for each in your code.

$new_image_array = array_shift($portfolio_gallery_image);
Ashish Patel
  • 3,551
  • 1
  • 15
  • 31