2

I've got an array of images attached to a post, and can output them sequentually just fine:

1 2 3 4 5 6 7

How can I get three images at a time from the array, then step one image forward to create the following arrays of three images:

1 2 3

2 3 4

3 4 5

4 5 6

and so on?

Here's my code:

global $rental;

$images = get_children( array(
    'post_parent' => $rental_id,
    'post_status' => 'inherit',
    'post_type' => 'attachment',
    'post_mime_type' => 'image',
    'order' => 'ASC',
    'orderby' => 'menu_order ID'
) );

if ($images) :

    $i = 0;

    foreach ($images as $attachment_id => $image) :

        $i++;

        $img_url = wp_get_attachment_url( $image->ID );

        ?>

        <div class="item <?php if($i == 1){echo ' active';} ?> class="<?php echo $i; ?>"">
            <img src="<?php echo $img_url; ?>" title="<?php echo $i; ?>" />
        </div>

    <?php endforeach; ?>
<?php endif;
  • Might be easier to help if we know what you are trying to accomplish? Are you trying to have multiple loops and output 3 images at a time? – czerspalace Dec 23 '16 at 1:30
2

You can split your array using the following

$array = array(1,2,3,4,5,6);
$number_of_elements = 3;

$count = count( $array );
$split = array();
for ( $i = 0; $i <= $count - 1; $i++ ) {
    $slices = array_slice( $array, $i , $number_of_elements);
    if ( count( $slices ) != $number_of_elements )
        break;

    $split[] = $slices;
}

print_r( $split );
  • I'm just trying to integrate my images into it though, and at the moment it's not working quite right. I'm a little stuck as to how to get it to pull the images. I've got $array = $images; set up now, and print_r( $slices ); outputs all of the meta related to each of the attachments. I just need to be able to get the URL of the image- any ideas on how I would achieve this? – warm__tape Jan 8 '17 at 22:25
  • I've updated the code, this should make it easier. You now have an array $split containing all the slices – Tunji Jan 8 '17 at 22:37
  • Thanks- I've replaced print_r( $split ) with: ` if ($split) : foreach ($split as $outeritem) : echo 'Outer Top <br/>'; foreach ($split as $inneritem) : echo 'Inner Top <br/>'; echo 'Image Url: ' . $inneritem . '<br/>'; echo 'Inner Bottom <br/>'; endforeach; echo 'Outer Bottom <hr/>'; endforeach; endif;` Which works fine and seems to output the structure as I want it. The last piece of the puzzle is getting $inneritem to output the URL of the image, rather than just the word array- any tips? – warm__tape Jan 9 '17 at 3:48
1

You can use array_chunk for a more elegant solution.

Example:

$array = array( 1, 2, 3, 4, 5, 6, 7, 8, 9 );

$chunks = array_chunk( $array, 3 );

The value of $chunks in the example above would be:

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

    [1] => Array
        (
            [0] => 4
            [1] => 5
            [2] => 6
        )

    [2] => Array
        (
            [0] => 7
            [1] => 8
            [2] => 9
        )

)

Short and sweet without the need for any loops.

  • Unfortunately that doesn't help- the chunks i'm after are 1 2 3, 2 3 4, 3 4 5 and so on. – warm__tape Jan 9 '17 at 1:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.