Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way of using wp_get_attachment_image() in wordpress

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"><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?

like image 607
Constant Meiring Avatar asked Jun 22 '10 01:06

Constant Meiring


People also ask

What is Wp_get_attachment_image?

wp_get_attachment_image( $attachment_id, $size, $icon, $attr ); If the attachment is an image, the function returns an image at the specified size. For other attachments, the function returns a media icon if the $icon parameter is set to true.

How do I find an image URL in WordPress?

Finding Your WordPress Image URLs from the FrontendOpen a page or post where the image is published and locate the image. After that, right-click on the image, and select 'Open image in new tab' option. Once the image opens in the new tab, look at the URL in the address bar. That is your WordPress image URL.

How do I get image data in WordPress?

$image_url_link = $image_src[0]; // Gives you image url. $image_width = $image_src[1]; // Gives you image width. $image_height = $image_src[2]; // Gives you image height. echo '<img src="' .


2 Answers

Actually, I don't think the accepted answer really answers the question.

Your problem is that you're passing in the post id (54 in your example; typically $post->ID in WP parlance) to wp_get_attachment_image(). As can be seen in the codex, you're supposed to use the attachment id (see $attachment_id below):

wp_get_attachment_image( $attachment_id, $size, $icon );

In other words, you've got to do something like this:

$image_attr = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'medium');
like image 110
rinogo Avatar answered Oct 19 '22 22:10

rinogo


wp_get_attachment_image function can accept four values as you can see:

wp_get_attachment_image ( int $attachment_id, string|array $size = 'thumbnail', bool $icon = false, string|array $attr = '' )

So i always use:

<?php echo wp_get_attachment_image( get_the_ID(), array('700', '600'), "", array( "class" => "img-responsive" ) );  ?>

Note: we can simply use get_the_ID() to pass id of active post. and here 700 is width and 600 is height of attachment image. we can also pass our class as array( "class" => "img-responsive" )

like image 29
Rinku Avatar answered Oct 19 '22 23:10

Rinku