使用 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']; ?>" />);
谢谢你的帮助
一个 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>
这样做,你不能通过 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>
我正在使用 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']; ?>" />);
谢谢你的帮助
一个 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>
这样做,你不能通过 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>