Ask your WordPress questions! Pay money and get answers fast! (more info)

for each loop not working with attachments in slider

  • SOLVED

Hey,

Trying to get a for each loop to work within a slider using Aqua Resizer script to resize the images. The images are being resized correctly, but the slider is only showing the same image for each slide - which seems to indicate that the correct number of slides are showing, but the incorrect image for each slide. Hope that makes sense. Here's the loop as I currently have it:


<div class="slick-slider">
global $wpdb;
$images = get_post_meta( get_the_ID(), 'siiimple_gallery', false );
$images = implode( ',' , $images );
// Re-arrange images with 'menu_order'
$images = $wpdb->get_col( "
SELECT ID FROM {$wpdb->posts}
WHERE post_type = 'attachment'
AND ID in ({$images})
ORDER BY menu_order ASC
" );


foreach ( $images as $attachment )
{
$attachment_url = wp_get_attachment_url($attachment->ID , 'full');
$attachment = aq_resize($attachment_url, 525, 350, false);
echo '<div class="slick-item"><img src="'.$image.'"/></div>';
}

</div>

Answers (5)

2014-08-20

Luis Abarca answers:

$wpdb->get_col returns an array.

Try this:

foreach ( $images as $attachment_ID ) {
$attachment_url = wp_get_attachment_url($attachment_ID , 'full');
$attachment = aq_resize($attachment_url, 525, 350, false);
echo '<div class="slick-item"><img src="' . $attachment . '"/></div>';
}

2014-08-20

Dbranes answers:

Hi,

It's unclear what the value of <em>$image</em> is in this line:

echo '<div class="slick-item"><img src="'.$image.'"/></div>';

I wonder if you want to replace it with <em>$attachment</em> ?


Dbranes comments:

<strong>More info:</strong>

From the source code, we find that the value of your <em>$attachment</em> variable is indeed the url of the resized image:


---clip---
// Return the output.
if ( $single ) {
// str return.
$image = $img_url;
} else {
// array return.
$image = array (
0 => $img_url,
1 => $dst_w,
2 => $dst_h
);
}

return $image;
---clip---


where <em>$single</em> is true by default.

You can always change it with the 5th input parameter of

aq_resize( $url, $width = null, $height = null, $crop = null, $single = true, $upscale = false )


So you were almost there, just need change the line:

echo '<div class="slick-item"><img src="'.$image.'"/></div>';


to:

echo '<div class="slick-item"><img src="'.$attachment.'"/></div>';

I hope this help solve the problem.

2014-08-20

timDesain Nanang answers:

try to change this part:

foreach ( $images as $attachment ){
$attachment_url = wp_get_attachment_url($attachment->ID , 'full');
$attachment = aq_resize($attachment_url, 525, 350, false);
echo '<div class="slick-item"><img src="'.$image.'"/></div>';
}

with:

foreach ( $images as $attachment ){
$attachment_url = wp_get_attachment_url($attachment->ID , 'full');
$attachment_src = aq_resize($attachment_url, 525, 350, false);
echo '<div class="slick-item"><img src="'.$attachment_src.'"/></div>';
}

2014-08-20

Arnav Joy answers:

Try replacing
$attachment = aq_resize($attachment_url, 525, 350, false);
echo '<div class="slick-item"><img src="'.$image.'"/></div>


With this

$imgsrc = aq_resize($attachment_url, 525, 350, false);
echo '<div class="slick-item"><img src="'.$imgsrc.'"/></div>

2014-08-20

PHPSmashCode answers:

Hello Justin,

Change for each like this

foreach ( $images as $attachment )
{
$attachment_url = wp_get_attachment_url($attachment->ID , 'full');
$image = aq_resize($attachment_url, 525, 350, false);
echo '<div class="slick-item"><img src="'.$image.'"/></div>';
}

https://github.com/syamilmj/Aqua-Resizer/wiki/Examples


Justin Young comments:

Hey Hari,

Thanks for the response. that didn't seem to work, though. I've got an answer here that worked, from Luis. I think it had something to do with $attachment->ID.

I'm going to put up that slider/video issue, but I'll send it your way first if you want to have a crack at it?

Thanks again Hari