0

I have a strange problem, when I try to enter the source of an image tag using PHP it shows me the following error in the inspector

<img src=(unknown) alt="">

this code fragment gives me the correct url, checked by seeing the CPanel and pasting and copying the address,but when I try to enter it via php the image is not shown

$image = wp_get_attachment_image_src( $post_thumbnail_id); 
//echo($image[0]);
?>
<img src="<?php $image[0]; ?>" alt="">

The next thing I did was an echo of image[0] and it gave me the url of the image, I copied it and pasted it in the tag and that's when it showed me the image

<img src="mysite.com/wp-content/uploads/2018/11/957a...150x150.jpg" alt="">

I saw the page in Incognito Window and I did not show the image either.

Any clues?

2 Answers 2

0

I think that you need echo the image, try this:

<php
$image = wp_get_attachment_image_src( $post_thumbnail_id ); 
//echo( $image[0] );
?>
<img src="<?php echo $image[0]; ?>" alt="">
1
  • thanks, now I will slowly dissapear in a sea of shame
    – Trauko
    Commented Nov 21, 2018 at 1:23
1

If you only want the URL of the image, use wp_get_attachment_image_url(). It saves you having to do the [0] thing:

<img src="<?php echo wp_get_attachment_image_url( $post_thumbnail_id ); ?>" alt="">

However, if you want to output an image tag for an attachment, you're much better off using wp_get_attachment_image():

echo wp_get_attachment_image( $post_thumbnail_id, 'full' );

This will give you the full <img> tag, but including the alt text, width & height attributes, and srcset attribute. The alt text in particular is important.

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.