logo

$20
Getting images attached to a post

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

Rick Bible | 05/01/10 at 11:18am | Edit


(1) Possible Answers Submitted...

  • avatar
    Last edited:
    05/01/10
    12:22pm
    Utkarsh Kukreti says:

    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

    }

    }

    Previous versions of this answer: 05/01/10 at 12:09pm | 05/01/10 at 12:10pm

    • 05/01/10 11:56am

      Rick Bible says:

      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, 'medium', 'alt="' . $post->post_title . '"'); ?>

    • 05/01/10 12:08pm

      Rick Bible says:

      <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.

    • 05/01/10 12:11pm

      Utkarsh Kukreti says:

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

    • 05/01/10 12:17pm

      Rick Bible says:

      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...

    • 05/01/10 12:19pm

      Rick Bible says:

      I see how now,

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

    • 05/01/10 12:19pm

      Utkarsh Kukreti says:

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

This question has expired.





Current status of this question: Completed