• Hi everyone, I am hitting my head against a brick wall with this one, and hope someone can help.

    I am using some custom content types to create an image gallery. I am using the below code to pull together all the related attachments and display the thumbnails as a link to the full size image.

    <?php
    			$gallery_images = get_custom_field('galleryImages:to_array');
    			foreach ($gallery_images as $galleryID) {
    				$thumb_img = wp_get_attachment_image_src( $galleryID, 'thumbnail' ); 	//thumbnail src
    				$full_img = wp_get_attachment_image_src( $galleryID, 'full' );			//full img src
    				$attachment = get_post( $galleryID );									//get attachment details
    				$description = $attachment->post_content;								//get image description
    				$caption = $attachment->post_excerpt;									//get image caption
    				?>
                    	<a href="<?php echo $full_img[0]; ?>" class="thumbLink" target="_blank"><img src="<?php echo $thumb_img[0]; ?>"></a>
    				<?php
    			}
    		?>

    That works nicely. Here’s where it get’s tricky… Notice I am setting two variables ($description and $caption). I would like to expose these two variables to Javascript, to be manipulated by jQuery. I have tried the below code, but the problem is that is changing the JS variable value each time and so at the end the value of the JS variables are those of the last image in the array, and I’ve lost all the other descriptions and captions.

    <?php
    			$gallery_images = get_custom_field('galleryImages:to_array');
    			foreach ($gallery_images as $galleryID) {
    				$thumb_img = wp_get_attachment_image_src( $galleryID, 'thumbnail' ); 	//thumbnail src
    				$full_img = wp_get_attachment_image_src( $galleryID, 'full' );			//full img src
    				$attachment = get_post( $galleryID );									//get attachment details
    				$description = $attachment->post_content;								//get image description
    				$caption = $attachment->post_excerpt;									//get image caption
    				?>
                    	<a href="<?php echo $full_img[0]; ?>" class="thumbLink" target="_blank"><img src="<?php echo $thumb_img[0]; ?>"></a>
                        <script>
    						var imgDesc = <?php echo json_encode($description); ?>;
    						var imgCaption = <?php echo json_encode($caption); ?>;
    						alert(imgDesc);
    						alert(imgCaption);
                        </script>
    				<?php
    			}
    		?>

    My end goal is that when a user clicks on one of the image thumbnails the full size image and the description both load in a dive on the same page. That’s why I need each images description stored.

    Can someone help?

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Dynamically get attachment description and caption’ is closed to new replies.