自定义字段背景后备图像

Custom field background fallback image

我正在使用自定义字段在我的 wordpress 博客的每个页面上显示不同的背景,如下所示:

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

但现在我想要一个 后备 图像,以便在没有设置图像时使用。

有人知道怎么做吗?

提前谢谢

我假设你的代码在循环中

<?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 }?>
Try this as your inline CSS:
<?php
    $ACFimg = get_field('yourimagefieldname');
    $default_img = anything.jpg; (the image you want to show if user do not put any)
?>

<div style="background-image:url(<?php echo (!empty ( $ACFimg )? $ACFimg['url']: $default_img ); ?>);" ></div>