Search code examples
phpwordpresscustom-fieldsadvanced-custom-fields

Custom field background fallback image


I'm using a custom field to show a different background on every page of my wordpress blog like this:

<div id="wrapper" style="background-image: url('<?php the_field('background'); ?>');">

But now i'd like to have a fallback image for when their is no image is set.

Anybody knows how to do this?

thnx in advance


Solution

  • I assume your code is inside the loop

    <?php if(isset(get_field('background'))){?>
    <div id="wrapper" style="background-image: url('<?php the_field('background'); ?>');
    <?php } else{?>
    <div id="wrapper" style="background-image: url('any_custom_image_path');
    
    <?php }?>