functions - Cant display an image via PHP in wordpress

admin2025-06-06  8

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

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

Share Improve this question asked Nov 21, 2018 at 0:59 TraukoTrauko 251 silver badge6 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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="">

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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749164637a316909.html

最新回复(0)