使用php

时间:2016-02-21 10:21:50

标签: php wordpress image background advanced-custom-fields

我使用wordpress和ACF插件添加图片:

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

我想在div中添加一个图像作为背景。 我在我的style.css中使用了这段代码,但它不起作用:

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

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

谢谢你的帮助

2 个答案:

答案 0 :(得分:0)

css文件包含CSS。只是CSS。您不能将html或PHP写入CSS文件。 如果要使用PHP生成CSS属性,则必须直接在PHP文件(视图)中使用<style>...</style>标记。例如:

<?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 :(得分:0)

这样做,你不能通过css添加alt,所以请改用标题,看看here, SO css background image alt attribute

`<div style="background-image: url( <?php echo $image['url']; ?>); height: 200px; width: 400px; border: 1px solid black;" title= "<?php echo $image['alt']; ?>">my DIV  with a background image:</div>