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 really hope this is a facepalm moment, b/c i'm getting frustrated with this. Making a quick loop to show certain attachment images. lifted it almost straight from the codex but wp_get_attachment_image() isn't behaving like I'd expect.

isn't the ID part of the wp_get_attachment_image the ID of the attachment? is that not the same as the post id of the attachment?

$args = array(
        'post_type' => 'attachment',
        'post_mime_type' => array('image','image/jpeg','image/png','image/gif'),
        'numberposts' => 5,
        'order' => 'ASC',
        'orderby' => 'post_title'
    );


$attachments = get_posts( $args );
     if ( $attachments ) {
        foreach ( $attachments as $attachment ) {
           echo wp_get_attachment_image( $attachment->ID, 'full' , true);
           echo '<p>' . $attachment->post_title . '</p>';
          }
     }

prints out the post titles but no images.

EDIT 1: yes I am definitely getting posts with my get_posts

some more debugging:

$attachments = get_posts( $args );
     if ( $attachments ) {
        foreach ( $attachments as $attachment ) {
           $image= wp_get_attachment_image( $attachment->ID, 'thumbnail' , false, array( 'id' => $attachment->ID ));
           echo $image;
           print_r(wp_get_attachment_image_src( $attachment->ID, 'thumbnail' , false, array( 'id' => $attachment->ID )));
           echo var_export($image);
           echo '<p>' . $attachment->post_title . '</p>';
          }
     }
}

returns this:

Array ( [0] => http://localhost/multi/wp-content/uploads/2011/11/FabricColllFabrics-150x150.jpg [1] => 100 [2] => 100 [3] => 1 ) 
false
FabricColllFabrics

for the first image... so wp_get_attachment_image_src is correctly finding the source, but wp_get_attachment_image is apparently returning false?

edit 2: and there's the face palm i was waiting on

at some point i must've been debugging something and for who knows what reason i put a

function wp_get_attachment_image_src($attachment_id, $size='thumbnail', $icon = false) {
  return false;

right at the beginning of wp_get_attachment_image

this is all localhost stuff, and i don't usually hack at core, but i guess at some point i was in there probably trying to backtrace something and forgot to clean up after myself. epic fail.

share|improve this question
    
What do you get if you just use wp_get_attachment_image( $attachment->ID )? –  Chip Bennett Mar 8 '12 at 19:39
1  
You should add your "solution" as answer, so you mark it later on and not leave it open. –  kaiser Mar 10 '12 at 18:08

3 Answers 3

You're obviously getting data returned for $attachments = get_posts(); otherwise, you wouldn't be able to output $attachment->post_title. So the issue must be with your wp_get_attachment_image() call.

The first thing I would check is: why are you setting the $icon parameter to true? This parameter is intended to output a mime-type icon, instead of the actual attachment image. Try setting it to false (or omitting the parameter altogether, since false is the default).

Other than that, it's a long-shot, but try omitting full, and let wp_get_attachment_image() attempt to output its default image size, thumbnail.

share|improve this answer
    
yes i was getting data returned for $attachments. thank you so much for your response. while the problem ended up being my own stupidity (like i thought it was) you and @kaiser both helped me finally find the problem. –  helgatheviking Mar 9 '12 at 16:15

Problem debugging:

If you var_export() the result of $image = wp_get_attachment_image( $attachment->ID, 'full' , true); and get string(0), then it's simply skipping, as the image wasn't received.

Debuggin deeper:

Just add the 4th argument as array to wp_get_attachment_image(): array( 'id' => $attachment->ID ); and then try the following in your functions.php file:

function debug_img_atts( $attr, $attachment )
{
    echo '<pre>';
    var_dump( $attr );
    var_dump( $attachment );
    echo '<pre>';
}
add_filter( 'wp_get_attachment_image_attributes', 'debug_img_atts', 20, 2 );

and see if your id is the one from your attachments.

Another problem could be that your image size isn't registered. The default image size is 'thumbnail'. Try to leave the 3rd arg empty and then use add_theme_support( 'post-thumbnails' ); to your functions.php and see if you get an output.

You could also try to use var_dump( wp_get_attachment_image_src( $attachment->ID ) );

A last solution would be to simply leave the post_mime_type parameter out and check what MIME type the actual attachment is, before dropping it back into your query. Use var_dump( get_post_mime_type( $attachment->ID ) ); to inspect it

share|improve this answer
    
thank you so much for your very detailed response. you definitely helped me find the offending piece of code –  helgatheviking Mar 9 '12 at 16:17
up vote 0 down vote accepted

solution: the the face palm i was waiting on

at some point i must've been debugging something and for who knows what reason i put a

function wp_get_attachment_image_src($attachment_id, $size='thumbnail', $icon = false) {
  return false;

right at the beginning of wp_get_attachment_image

this is all localhost stuff, and i don't usually hack at core, but i guess at some point i was in there probably trying to backtrace something and forgot to clean up after myself. epic fail.

share|improve this answer

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.