ACF标题背景

时间:2015-06-29 13:32:07

标签: wordpress advanced-custom-fields

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

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

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

我改变了一点。您遇到了问题,因为如果没有'header',您就会传入图片元素(您应该只传递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>