dpdp42233 2016-02-21 10:21
浏览 1440
已采纳

使用php添加背景图片

I'm using wordpress and ACF plugins to add an image :

http://www.advancedcustomfields.com/resources/image/

I would like to add an image in a div as a background. I used this code in my style.css but it doesn't work :

background-image: url(<?php $image = get_field('image_projet');?> <img 

src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />);

Thanks for you help

  • 写回答

2条回答 默认 最新

  • duanlei4759 2016-02-21 10:43
    关注

    A css file contains CSS. Just CSS. You can't write html or PHP into a CSS file. If you want to generate a CSS property with PHP, you must use the <style>...</style> tags directly in your PHP file (the view). For instance :

    <?php $image = get_field('image_projet'); // fetch the ACF field ?>
    
    <html>
        <head>
            <title>Page's Title</title>
            <style>
                .your-div {
                    background-image: url('<?php echo $image; ?>');
                }
            </style>
        </head>
        <body>
            <!-- this div uses the $image URL as a background. -->
            <div class="your-div">
                Lorem Ipsum.
            </div>
        </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程