$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>functions - Cant display an image via PHP in wordpress|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

functions - Cant display an image via PHP in wordpress

matteradmin8PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far