dpdp42233 2016-02-21 02:21
浏览 1454
已采纳

使用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 02: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部