Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
What are you getting with the "What I WANT to work" and "Also tried" code snippets? –  tollmanz Aug 25 '11 at 22:06
    
your function works for me with the Also tried code, what's the issue? –  Milo Aug 25 '11 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 '11 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 '11 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 '11 at 13:49
add comment

1 Answer

up vote 1 down vote accepted

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!

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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