PHP: Pluck out variables inside echo

I have an array of Wordpress attachment IDs. I want to use wp_get_attachment_metadata() to get a few pieces of data for a slider.

The code below would make the most sense to me, but I’m breaking the site with it.

What’s the right way to pull out data from the array and echo it out?

$gallery_array is an array of image IDs
wp_get_attachment_metadata gets an array containing all of the images meta.

The part that’s causing problems is the

echo "... data-rsw=\"$img_meta['sizes']['medium']['width']\" ..."

part. I’m assuming there’s something wrong with my syntax, but I don’t know what.

foreach ($gallery_array as $gallery_img){
    	$img_meta = wp_get_attachment_metadata($gallery_img);
    	var_dump($img_meta);
    	
		echo "
   			<a class=\"rsImg\" data-rsw=\"$img_meta['sizes']['medium']['width']\"   data-rsBigImg=\"https://dimsemenov.com/plugins/royal-slider/img/paintings/1.jpg\" href=\"https://dimsemenov.com/plugins/royal-slider/img/paintings/700x500/1.jpg\">Vincent van Gogh - Still Life: Vase with Twelve Sunflowers<img width=\"96\" height=\"72\" class=\"rsTmb\" src=\"https://dimsemenov.com/plugins/royal-slider/img/paintings/t/1.jpg\" /></a>
			";
    };

I prefer using PHP HereDoc Strings because they eliminate a lot of syntax and make the string far more readable:

	$tmp = <<< ____EOT
   			<a 
   				class 	      = 'rsImg'
   				data-rsw 	  = '{$img_meta['sizes']['medium']['width']}'   
   				data-rsBigImg = 'https://dimsemenov.com/plugins/royal-slider/img/paintings/1.jpg' 
   				href  	 	  = 'https://dimsemenov.com/plugins/royal-slider/img/paintings/700x500/1.jpg'
   			>
   				Vincent van Gogh - Still Life: Vase with Twelve Sunflowers
   				<img 
   					width = '96' height='72' 
   					class = 'rsTmb' 
   					src   = 'https://dimsemenov.com/plugins/royal-slider/img/paintings/t/1.jpg' 
   				>
   			</a>
____EOT;
echo '<pre>'; print_r($tmp); echo '</pre>';

:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.