Horje
Link large attachment to full-size attachment - Wordpress Solution
[Solved] - Link large attachment to full-size attachment - Wordpress Solution
Try following codes
Hey guys/girls. I'm using the following code to loop through the images attached to a page, and display the large sized images that WordPress creates. I want to link the large images to the full-size images, for Thickbox usage. How do I do this? Here's my code:
<?php
	$args = array(
	'order'          => 'ASC',
	'orderby'        => 'menu_order',
	'post_type'      => 'attachment',
	'post_parent'    => $post->ID,
	'post_mime_type' => 'image',
	'post_status'    => null,
	'numberposts'    => -1,
	);
	$attachments = get_posts($args);
	if ($attachments) {
	    foreach ($attachments as $attachment) { ?>

	    <?php echo wp_get_attachment_image($attachment->ID, 'large', false, false);
		}
	} 
?>


Solution - 1

Change:

echo wp_get_attachment_image($attachment->ID, 'large', false, false);
To:
echo '<a href="'.wp_get_attachment_image_src( $attachment->ID, 'full', false, false ).'"><img src="'.wp_get_attachment_image_src( $attachment->ID, 'large', false, false ).'" alt="img" /></a>';


Solution - 2

Not familiar with Thickbox, but if you want to add a href around the image: The new loop would be:


foreach ( $attachments as $attachment ) {
	$full_image = wp_get_attachment_image_src( $attachment->ID, 'full', false, false );
	list ( $full_src, $full_width, $full_height ) = $full_image;
	echo '<a href="'.$full_src.'">';
	echo wp_get_attachment_image( $attachment->ID, 'large', false, false) );
	echo '</a>';
}
Ok, that's what you get when you answer and code at the same, almost the same answer was posted already, With my version you keep the alt, title, width, height attributes of the image.


Solution - 3


//Put images from post in to array $images. Order by menu order defined in post gallery
	$images = get_children( array('post_parent' => $post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID') );
	if (isset($images)) {
	$count=0;	
	//go through all images in array $images
		foreach( $images as $image ) {
			$imageID = $image->ID;
			$medImageSrc = wp_get_attachment_image_src($imageID, $size='large', $icon = false);
			$largeImageSrc = wp_get_attachment_image_src($imageID, $size='full', $icon = false);
			echo"<a href='$largeImageSrc[0]' rel='thickbox'><img src='$medImageSrc[0]' alt='Image' /></a>";
			$count++;
		} //foreach
	}
this will display: Image It's really clean and easy to manipulate. Hope it helps!

Date: 2022-10-21 05:20:34
Category: Wordpress
More for You