本文介绍了ACF标头背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在使用高级自定义字段在标题中显示背景图像。现在,如果没有定义ACF图像,我想显示一个默认图像。我尝试了这段代码,但是没有用(图像仅显示是否已设置):

I am using Advanced Custom Fields to display a background image in my header. Now I want to display a default image if there is no ACF-image defined. I tried this code, but it didn't work (image only shows if it is set):

<?php wp_head(); ?>

    <?php if(get_field('header')) {
        $image = get_field('header');
    } else {
       $image = '<img src="#absolute-path-to-my-image">';
    }
    ?>

    <style type="text/css">
         #inner-header{
             background-image: url('<?php echo $image['url']; ?>');
             background-position:center;
             background-repeat:no-repeat;
             background-size:cover;
         }
    </style>

感谢您的帮助。

推荐答案

我会稍作改动。您遇到了问题,因为如果不存在'header'(如果只应传递 src ):

I'd change things up a bit. You're experiencing problems because you're passing an image element in if there's no 'header' present (you should only be passing the src):

<?php
    // Get the field
    $image = get_field('header');
    // Does the field exist ? src : default
    $image_src = $image ? $image['url'] : 'http://example.com/default/src.jpg';
?>

<style>
    #inner-header {
        background: url(<?php echo $image_src; ?>) center / cover no-repeat;
    }
</style>

这篇关于ACF标头背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 03:52