【问题标题】:ACF Header BackgroundACF 标题背景
【发布时间】:2015-06-29 21:47:43
【问题描述】:

我正在使用高级自定义字段在我的标题中显示背景图像。现在,如果没有定义 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>

感谢您的帮助。

【问题讨论】:

    标签: wordpress advanced-custom-fields


    【解决方案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>
    

    【讨论】:

      猜你喜欢
      • 2018-10-11
      • 2018-09-09
      • 2015-10-11
      • 2018-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-11
      • 2013-07-02
      相关资源
      最近更新 更多