Php 我想在CSS中的WordPress中添加背景图像

Php 我想在CSS中的WordPress中添加背景图像,php,css,wordpress,background-image,Php,Css,Wordpress,Background Image,我的html 我想在这里添加背景图像 .banner-area { position: relative; background: #f0fffa; padding: 30px 0; } 我认为PHP代码不能在CSS中运行。我该怎么做 假设您的CSS文件位于WordPress目录中的某个位置,例如在wp content/themes/foobar/example.CSS中,将该文件重命名为wp content/themes/foobar/example.CSS.php(附加.php

我的html

我想在这里添加背景图像

.banner-area {
  position: relative;
  background: #f0fffa;
  padding: 30px 0;
}

我认为PHP代码不能在CSS中运行。我该怎么做

假设您的CSS文件位于WordPress目录中的某个位置,例如在
wp content/themes/foobar/example.CSS
中,将该文件重命名为
wp content/themes/foobar/example.CSS.php
(附加.php扩展名!),并在文件开头添加以下代码:

.banner-area:before {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  content: "";
  background: url(../images/bg03.png) no-repeat;
  background-size: cover;
}
然后,将CSS文件的源路径更改为:
wp content/themes/foobar/example.css.php

我解决了这个问题。这是最好且简单的解决方案

.banner-area:before {

  left: 0;
  content: "";
  background: url(<?= $pathToBackgroundImage ?>) no-repeat;
}

你错过了一些代码?()我无法理解如何在这里应用图像路径。谢谢你的努力,但我找到了一个简单的解决方案。
<?php

header('Content-Type: text/css; charset=utf-8');
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
require_once($_SERVER['DOCUMENT_ROOT'].'/wp-load.php');

$pathToBackgroundImage = '../images/bg03.png'; // <- get image path dynamically
?>
.banner-area:before {

  left: 0;
  content: "";
  background: url(<?= $pathToBackgroundImage ?>) no-repeat;
}
<div style="background: url('<?php echo get_template_directory_uri();?>/img/bg03.png')no-repeat;background-size: cover;">