Echoed image URL has '/'s replaced with '"=" 's (2 posts)

  1. Matt
    Posted 3 years ago #

    I've written a shortcode that inserts a summary of a defined post or page including an image attached to the said post or page. It works, except when it comes to the div box containing the image, the image url is returned with spaces & quotation marks instead of forward slashes (i.e. http://example.com/subdirectory/image.jpg is returned as http:="" example.com="" subdirectory="" image.jpg="")

    Here is the relevant bit of code:

    `$attachments = get_children( array(
    'numberposts' => 1,
    'order'=> 'DESC',
    'orderby' => 'Description',
    'post_mime_type' => 'image',
    'post_parent' => $id,
    'post_status' => null,
    'post_type' => 'attachment'
    if ($attachments) {
    foreach($attachments as $attachment) {
    $image_attributes = wp_get_attachment_image_src( $attachment->ID, 'medium' );
    echo '<div style="background-image: url("',"$image_attributes[0]",'" class="filled"></div>';

    ...which returns the following:

    <div style="background-image: url(" http:="" example.com="" subdirectory="" wp-content="" uploads="" 2012="" 11="" our-selection-282x300.jpg"="" class="filled"></div>

    Does anyone know why this is?

    Thanks in advance

  2. bcworkz
    Posted 3 years ago #

    You should not echo shortcode content. As you have seen, it causes unpredictable behavior. Instead, accumulate output into a variable, then return it from the shortcode function.

Topic Closed

This topic has been closed to new replies.

About this Topic