views:

16

answers:

1

I'm looking for the correct way of using wp_get_attachment_image().

The following code:

<?php
    $args = array(
        'type' => 'attachment',
        'category_name' => 'portfolio'
        );
    $attachments = get_posts($args);
    print_r($attachments);
?>

Generates the following result:

Array
(
    [0] => stdClass Object
        (
            [ID] => 54
            [post_author] => 1
            [post_date] => 2010-06-22 00:32:46
            [post_date_gmt] => 2010-06-22 00:32:46
            [post_content] => <a href="http://localhost/wordpress/wp-content/uploads/2010/06/Capture.jpg"&gt;&lt;img class="alignnone size-medium wp-image-55" title="Capture" src="http://localhost/wordpress/wp-content/uploads/2010/06/Capture-300x114.jpg" alt="" width="300" height="114" /></a> 
            [post_title] => Our Own Site
            [post_excerpt] => 
            [post_status] => publish
            [comment_status] => open
            [ping_status] => open
            [post_password] => 
            [post_name] => our-own-site
            [to_ping] => 
            [pinged] => 
            [post_modified] => 2010-06-22 00:40:22
            [post_modified_gmt] => 2010-06-22 00:40:22
            [post_content_filtered] => 
            [post_parent] => 0
            [guid] => http://localhost/wordpress/?p=54
            [menu_order] => 0
            [post_type] => post
            [post_mime_type] => 
            [comment_count] => 0
            [filter] => raw
        )
)

The following, however, doesn't return anything.

<?php
    echo wp_get_attachment_image(54, array('300', '300'));
?>

What am I doing wrong here?

A: 

The function wp_get_attachment_image only gets an image that was uploaded to wordpress, it doesn't output an image in the content of the post.

You have to output the content of the post for your example image.

Like: echo $attachments['post_content'];

Aaron Harun
So Wordpress doesn't have an internal function to get an uploaded image at a specific size?
Constant M
No, the function you gave is the correct one, but you need to pass it the idea of the attachment itself not a post.
Aaron Harun
Ah okay thanks. But it should still return the image with size nearest to the specified size, shouldn't it?
Constant M
Yes, when loading attachments it will.
Aaron Harun
The above is an attachment, and it doesn't load it though. Could this be a bug?
Constant M
Look at the post_type. It isn't an attachment.
Aaron Harun