自定义字段背景回退图像



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

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

但现在我希望有一个回退图像,用于在没有设置图像的情况下。

有人知道怎么做吗?

thnx提前

我假设您的代码在循环中

<?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>

最新更新