0

I'm using wp_get_attachment_image_src to retrieve image URL.

Codex is simple for this function and starting from an ID on my database I used wp_get_attachment_image_src in this way:

$image_url = wp_get_attachment_image_src(2447);

echo $image_url[0];

where 2447 is an ID of an attachment in my wp_posts db table (already checked).

Everything seems correct, but trying to execute the code the wp_get_attachment_image_src returns always false.

Any suggestion?

1
  • try using wp_get_attachment_url(2447)
    – Ben Casey
    Mar 8, 2017 at 20:29

3 Answers 3

1

In function passing a attachment id or thumbnail id NOT post id.

Try this

<?php 

      global $post;
      $attch_id = get_post_thumbnail_id( $post->ID );
      $url = wp_get_attachment_image_src($attch_id);
      echo "<img src='".$url[0]."' />";

    ?>

I hope is useful.

0

Try in this way and let me know if it works or not ..

<?php
$my_attachment = 2447;
$image_url = wp_get_attachment_image_src( $my_attachment );?>
<img src="<?php echo $image_url[0] ?>" />
?>
5
  • Thanks @Rohil_PHPBeginner. Unfortunately nothing changed. I can't uderstand why, it's really simple PHP and really "stupid" function...
    – Pepozzo
    Sep 26, 2014 at 9:01
  • it worked or not ?? If yes then I edit my answer with explanation. Sep 26, 2014 at 9:02
  • No, your suggestion did not work.
    – Pepozzo
    Sep 26, 2014 at 9:14
  • Put that condition if( $image_url ) {} and try to debug it and see if we are true or not. Sep 26, 2014 at 9:18
  • It returns false.
    – Pepozzo
    Sep 26, 2014 at 9:23
0

Try it like this

$image_url = wp_get_attachment_image_src( $attachment_id = 2447 );
if ( $image_url ) : ?>
    <img src="<?php echo $image_url[0]; ?>" width="<?php echo $image_url[1]; ?>" height="<?php echo image_url[2]; ?>" />
<?php endif; ?>

SYA

Your Answer

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

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