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

Getting images attached to a post WordPress

  • SOLVED

I use the code below, which works great,
function my_attachment_gallery($postid=0, $size='thumbnail', $attributes='') {
if ($postid<1) $postid = get_the_ID();
if ($images = get_children(array(
'post_parent' => $postid,
'post_type' => 'attachment',
'order' => 'DESC',
'numberposts' => 0,
'post_mime_type' => 'image',)))
foreach($images as $image) {
$attachment=wp_get_attachment_image_src($image->ID, $size);
?><div class="feature"><img src="<?php echo $attachment[0]; ?>" <?php echo $attributes; ?> /></div><?php
}
}

I then add this to my post template
<?php my_attachment_gallery(0, 'medium', 'alt="' . $post->post_title . '"'); ?>
With that I can choose whether to show thumb, medium, large or full. What I need to be included here is that when the image is clicked I get the attachment image (large or full). I know gallery does this, but I need more control here in showing all images attached to the post. My goal is to design a "slideshow" within <div class="feature"><img src="<?php echo $attachment[0]; ?>" <?php echo $attributes; ?> /></div>
the featured div

Answers (1)

2010-05-01

Utkarsh Kukreti answers:

Something like this?
function my_attachment_gallery($postid=0, $size='thumbnail', $attributes='') {

if ($postid<1) $postid = get_the_ID();

if ($images = get_children(array(

'post_parent' => $postid,

'post_type' => 'attachment',

'order' => 'DESC',

'numberposts' => 0,

'post_mime_type' => 'image',)))

foreach($images as $image) {

$attachment=wp_get_attachment_image_src($image->ID, $size);
$full_attachment=wp_get_attachment_image_src($image->ID, 'full');

?><div class="feature"><a href="<?php echo $full_attachment[0]; ?>"><img src="<?php echo $attachment[0]; ?>" <?php echo $attributes; ?> /></a></div><?php

}

}


Rick Bible comments:

not quite, on the post page I can control what image size to show, I use thumbs or medium, which is still functioning fine with your code <?php my_attachment_gallery(0, 'medium', 'alt="' . $post->post_title . '"'); ?>
but I need to be able to click the image and show a larger one, using basically the same method. Your code works but the image attachment (i use a lightbox) is the same size as whatever images size I set here<?php my_attachment_gallery(0, '<strong>medium</strong>', 'alt="' . $post->post_title . '"'); ?>


Rick Bible comments:

<div class="feature"><a href="http://fabrics/wp-content/uploads/2010/03/citrus-359x270.jpg"><img src="http://fabrics/wp-content/uploads/2010/03/citrus-359x270.jpg" alt="cotton burlap" /></a></div>

This is the html output for your code... I need to control the "link to" size.


Utkarsh Kukreti comments:

I made the link to point to the full sized image now. Please try it out.


Rick Bible comments:

just about there... <?php echo $full_attachment[0]; ?>
So, if I want to call the large attachment... I tried <?php echo $large_attachment[0]; ?> which does not work... resolve this and we are done...


Rick Bible comments:

I see how now, $full_attachment=wp_get_attachment_image_src($image->ID, 'large'); worked. This would be how correct? works anyway.


Utkarsh Kukreti comments:

I declared full_attachment above as
$full_attachment=wp_get_attachment_image_src($image->ID, 'full');

If you need large_attachment also, add this after the full_attachment declaration line.
$large_attachment=wp_get_attachment_image_src($image->ID, 'large');