Strange Behavior for PHP Embedded in Img ‘src’ Attribute
-
I would like to dynamically display an image using a WordPress template without either relying upon a featured image or an Advanced Custom Fields image field. Here is the coder in my ‘header.php’ template thus far:
<?php $srcImage = wp_get_attachment_image('254', 'medium'); $imgSrcSet = array( 'Large' => wp_get_attachment_image_srcset('254', 'large'), 'Full Width' => wp_get_attachment_image_srcset('254', 'full-width') ); ?> <img src="<?php echo $srcImage; ?>" srcset="<?php echo $imgSrcSet; ?>" alt="" ?>My ‘front-page.php’ template includes this header programmatically:
<?php get_header(); ?>When I render to my web browser, the image appears in its correct image size, but right after the image the following text appears:
" srcset="Array" alt="" />When I inspect the image in my web browser, it reveals the following code:
<img src="<img width=" 300"="" height="200" class="attachment-medium size-medium" alt="" loading="lazy" srcset="http://wp-image-size-test.local/wp-content/uploads/2020/09/book_5472x3648-300x200.jpg 300w, http://wp-image-size-test.local/wp-content/uploads/2020/09/book_5472x3648-1024x683.jpg 1024w, http://wp-image-size-test.local/wp-content/uploads/2020/09/book_5472x3648-768x512.jpg 768w, http://wp-image-size-test.local/wp-content/uploads/2020/09/book_5472x3648-1536x1024.jpg 1536w, http://wp-image-size-test.local/wp-content/uploads/2020/09/book_5472x3648-2048x1365.jpg 2048w" sizes="(max-width: 300px) 100vw, 300px">The srcset attribute value includes the various WP auto-generate image versions and their default sizes, but the src attribute does not depict the image’s path in my site.
Your feedback is appreciated. Thank you.
The topic ‘Strange Behavior for PHP Embedded in Img ‘src’ Attribute’ is closed to new replies.