Displaying Image as Background

Support General Displaying Image as Background

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #1630
    tone4407tone4407
    Participant

    Hi. I'm trying to get a meta box field and insert in into my HTML as a background image. Here is my code:

    <?php 
    $images = rwmb_meta( 'meta-about-image', 'type=image' );
    foreach ( $images as $image )
    {
    echo "<a href='#' style='background: url('{$image['full_url']}');'></a>";
    }
    ?>

    On the front end, the HTML looks like this:

    <a href="#" style="background: url(" http:="" localhost="" decoypro="" wp-content="" uploads="" 2015="" 10="" scott-about.jpg');'=""></a>

    Obviously the full URL is not being added correctly. Any ideas why?

    #1634
    Anh TranAnh Tran
    Keymaster

    The problem is you're using single quote twice (for style attribute and for url in background), that makes the HTML invalid. You can omit the quote for url, like this:

    <?php 
    $images = rwmb_meta( 'meta-about-image', 'type=image' );
    foreach ( $images as $image )
    {
        echo "<a href='#' style='background: url({$image['full_url']});'></a>";
    }
    ?>
    #1637
    tone4407tone4407
    Participant

    Ugh. Can't believe I missed that. A second pair of eyes always helps, thanks!

Viewing 3 posts - 1 through 3 (of 3 total)
  • The topic ‘Displaying Image as Background’ is closed to new replies.