• 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="https://wp-image-size-test.local/wp-content/uploads/2020/09/book_5472x3648-300x200.jpg 300w, https://wp-image-size-test.local/wp-content/uploads/2020/09/book_5472x3648-1024x683.jpg 1024w, https://wp-image-size-test.local/wp-content/uploads/2020/09/book_5472x3648-768x512.jpg 768w, https://wp-image-size-test.local/wp-content/uploads/2020/09/book_5472x3648-1536x1024.jpg 1536w, https://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.

    • This topic was modified 4 years, 5 months ago by aspsa.
    • This topic was modified 4 years, 5 months ago by aspsa.
Viewing 1 replies (of 1 total)
  • You need to read up on what those functions do.
    Obviously, wp_get_attachment_image is returning the entire img tag, not just the src. And PHP has never echoed an array as anything other than Array. The srcset is actually added by WordPress in a separate action.

Viewing 1 replies (of 1 total)
  • The topic ‘Strange Behavior for PHP Embedded in Img ‘src’ Attribute’ is closed to new replies.