Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Link large attachment to full-size attachment WordPress

  • SOLVED

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);
}
}
?>

Answers (3)

2010-09-24

MagoryNET answers:

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


Dan Davies comments:

That just returns "Array"


MagoryNET comments:

Sorry, I misread the documentation. Corrected code:


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

2010-09-24

Peter van der Does answers:

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.

2010-09-24

chocks answers:


//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:
<a href="/full-image.jpg" rel="thickbox"><img src="/full-image.jpg" alt="Image"></a>

It's really clean and easy to manipulate. Hope it helps!