使用php添加背景图片(Add background image using php)

我正在使用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']; ?>" />);

谢谢你的帮助

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

最满意答案

一个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>

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>

更多推荐