0

I am trying to set background image from meta value.I am successful also.But if the meta image is not set the out put

<div id="pricing" style="background: url('')"></div>

But I do not want to show background: url('') if there is not meta value is set.My code to get image

        $bgimage= get_post_meta( $post->ID, 'settings_image', true );
        $img=background-image: url('.wp_get_attachment_url( $bgimage ).');

To display style="<?php echo $img;?>" I have tried

    if(isset($bgimage)){
    $img=background-image: url('.wp_get_attachment_url( $bgimage ).');
    }
1

try using an empty condition

<?php
$bgimage= get_post_meta( $post->ID, 'settings_image', true );
if (!empty($bgimage)) {
    $imgURL = wp_get_attachment_url( $bgimage );
   ?>background: url('<?php echo $img ?>')<?php } ?>

That should work as it checks to see if there is anything stored in the meta and will not print the background: url('') if there is no data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.