2

I'm building a custom carousel for a client. I've got a function which gets blocks of three images from all of the images attached to a post:

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'
) );

$array = $images;
$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;
}

if ($split) :
    foreach ($split as $outeritem) :    
        echo '<div class="Outer Top">';
            foreach ($split as $inneritem) :
                echo '<div class="Inner Top">';
                echo '<img src="' . $inneritem . '">';
                echo '</div>';
             endforeach;
        echo '</div>';
    endforeach;
endif;

//print_r( $split );

All I need to finalize this is to replace inneritem with the URL of the image. The data is all there in an array, and as you can see I just need to pull the value of guid for each item. The array below comes from uncommenting the print_r( $split ); and I've removed all the extraneous data for the sake of tidiness:

Array (
    [0] => Array (
        [0] => WP_Post Object (
            [ID] => 120
            [guid] => http://******/wp-content/uploads/2016/12/T15923-11-1-1.jpg
        )
        [1] => WP_Post Object (
            [ID] => 121
            [guid] => http://******/wp-content/uploads/2016/12/T15923-12-1-1.jpg
        )
        [2] => WP_Post Object (
            [ID] => 122
            [guid] => http://******/wp-content/uploads/2016/12/T15898.jpg
        )
    )
    [1] => Array (
        [0] => WP_Post Object (
            [ID] => 121
            [guid] => http://******/wp-content/uploads/2016/12/T15923-12-1-1.jpg
        )
        [1] => WP_Post Object (
            [ID] => 122
            [guid] => http://******/wp-content/uploads/2016/12/T15898.jpg
        )
        [2] => WP_Post Object (
            [ID] => 123
            [guid] => http://******/wp-content/uploads/2016/12/T15923-13-1-1.jpg
        )
    )
    [2] => Array (
        [0] => WP_Post Object (
            [ID] => 122
            [guid] => http://******/wp-content/uploads/2016/12/T15898.jpg
        )
        [1] => WP_Post Object (
            [ID] => 123
            [guid] => http://******/wp-content/uploads/2016/12/T15923-13-1-1.jpg
        )
        [2] => WP_Post Object (
            [ID] => 124
            [guid] => http://******/wp-content/uploads/2016/12/T15923-14-1.jpg
        )
    )
)
  • What is this array output you've given us? Is it of $split variable? – Howdy_McGee Jan 9 '17 at 20:54
  • The array you posted above, is that the inneritem or outeritem? – Tunji Jan 9 '17 at 21:33
  • Why not use get_permalink() – Benoti Jan 9 '17 at 21:37
  • The array above is the inneritem. – warm__tape Jan 9 '17 at 21:38
  • @Benoti get_permalink() gets the permalink of the parent post so doesn't help. – warm__tape Jan 9 '17 at 21:42
1

You should be able to rewrite what you have about and also use get_permalink as @Benoti stated while omitting the $split array.

get_permalink accepts either the Post ID or a post object.

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'
) );

$array = $images;
$number_of_elements = 3;
$count = count( $array );

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

    echo "<div class='Outer Top'>";
    foreach( $slices as $inneritem ) {
        $link = wp_get_attachment_url( $inneritem->ID );
        echo "<div class='Inner Top'>";
        echo "<img src=' $link  '>";
        echo "</div>";
    }
    echo "</div>";
}
  • That's getting closer, but it links to the attachment page rather than the media file. – warm__tape Jan 9 '17 at 21:54
  • 1
    I've updated my answer. You really don't need to have the multiple foreach's – Tunji Jan 9 '17 at 22:01
  • Just needed to swap wp_get_attachment_image_url for wp_get_attachment_url. Also, it creates an extra empty instance of <div class='Outer Top'></div> at the end. Any ideas on how to get rid of that? – warm__tape Jan 10 '17 at 1:56
  • Most likely because I'm adding the outer div before checking the break clause. I've updated my answer – Tunji Jan 10 '17 at 5:21
3

I didn't test anything and read your code, but it seems that you can get_permalink() as I was told in the comment but it's true that you will get the attachment page not its url.

You can access to the object ID, guid easily

wp_get_attachment_url($inneritem[$i]->ID);

So

if ($split) :
foreach ($split as $outeritem) :    
    echo '<div class="Outer Top">';
    $i=0;
        foreach ($split as $inneritem) :
            echo '<div class="Inner Top">';
            echo '<img src="' . wp_get_attachment_url($inneritem[$i]->ID) . '">';
            echo '</div>';
            $i++;
         endforeach;
    echo '</div>';
    endforeach;
endif;

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.