0

I'm trying to create a function to be placed in a template page to output the post's attached images. I would like to have the images display as the theme's specified thumbnail size and be linked to the full-size image so that I can give them a lightbox-triggering class (in this case thickbox).

I currently can only get an image to output using the_attachment_link which links and displays the image but doesn't let me customize it like I need to in order to add the class.

Here is my current code, including what works, what I think should work, and another solution I've tried:

function product_images() {
global $post;
    $attachments = get_posts( array(
        'post_type' => 'attachment',
        'posts_per_page' => 0,
        'post_parent' => $post->ID,
        'exclude' => get_post_thumbnail_id()
    ) );

    if ( $attachments ) {
        $content .= '<div class="productimgs">';
        foreach ( $attachments as $attachment ) {
            // What works to display image but doesn't allow customizing output
            $imgthumb = the_attachment_link($attachment->ID, false);
            $content .= $imgthumb;

            /*What I WANT to work!
            $imgthumb = wp_get_attachment_url($attachment->ID);
            $content .= '<a class="thickbox" href="'.$imgthumb.'"><img class="productimg" src="'.$imgthumb.'" /></a>';
            */

            /* Also tried
            $imgthumb = wp_get_attachment_image_src( $attachment->ID, 'thumbnail' );
            $imgfull = wp_get_attachment_image_src( $attachment->ID, 'full' );
            $content .= '<a class="thickbox" href="'.$imgfull[0].'"><img class="productimg" src="'.$imgthumb[0].'" /></a>';
            */
        }
        $content .= '</div>';
    }
return $content;

}

Thanks in advance for any help! I've been trying to get it going for several hours.

I've looked at several other articles on this site but none were the same problem. I've also poured over the Digging into Wordpress article about image tags.

5
  • What are you getting with the "What I WANT to work" and "Also tried" code snippets?
    – tollmanz
    Aug 25, 2011 at 22:06
  • your function works for me with the Also tried code, what's the issue?
    – Milo
    Aug 25, 2011 at 22:07
  • 1
    What do you get if you var_dump() return of what doesn't work, especially wp_get_attachment_img_src()? Do you have WP_DEBUG enabled?
    – Rarst
    Aug 25, 2011 at 22:08
  • @tollmanz - I've gotten those codes to work previously when retrieving images inside the main loop, but for some reason they weren't working in this function
    – 5t3ph
    Aug 26, 2011 at 13:36
  • @Rarst - Thanks for the debug and var_dump suggestions. I did both on the wp_get_attachment_img_src(). It did output correctly on the var_dump which led me to echo instead of return and voila! Thanks!
    – 5t3ph
    Aug 26, 2011 at 13:49

1 Answer 1

1

Thanks to @Rarst who led me in the right direction to answer this question!

What do you get if you var_dump() return of what doesn't work, especially wp_get_attachment_img_src()? Do you have WP_DEBUG enabled? – Rarst

function product_images() {
global $post;
    $attachments = get_posts( array(
        'post_type' => 'attachment',
        'posts_per_page' => 0,
        'post_parent' => $post->ID,
        'exclude' => get_post_thumbnail_id()
    ) );

    if ( $attachments ) {
        // $content needed to be just = not .= to start out the variable
        $content = '<div class="productimgs">';
        foreach ( $attachments as $attachment ) {
            // The correct function to retrieve the needed URLs
            $imgthumb = wp_get_attachment_image_src( $attachment->ID, 'thumbnail' );
            $imgfull = wp_get_attachment_image_src( $attachment->ID, 'full' );
            $content .= '<a class="thickbox" href="'.$imgfull[0].'"><img class="productimg" src="'.$imgthumb[0].'" /></a>';

        }
        $content .= '</div>';
    }
    // Most importantly, this needed to be an echo not a return
echo $content;
}

Thanks again Rarst for leading me to the answer!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.