php行在css中,但css来自php

php行在css中,但css来自php,php,css,wordpress,Php,Css,Wordpress,我有一个问题,我想在css行中添加一个get_template_directory_uri()。此Css行位于box.php文件中 $box_first = 'linear-gradient(rgba(0, 175, 227, .8), rgba(0, 175, 227, .8)), url("/wp/wp-content/themes/my_theme/inc/img/boxes/box2.png") center;'; 我也尝试过创建一个变量并将其放入 $tpl_dir

我有一个问题,我想在css行中添加一个get_template_directory_uri()。此Css行位于box.php文件中

$box_first = 'linear-gradient(rgba(0, 175, 227, .8), rgba(0, 175, 227, .8)),
url("/wp/wp-content/themes/my_theme/inc/img/boxes/box2.png") center;';
我也尝试过创建一个变量并将其放入

$tpl_dir = echo get_template_directory_uri() . 
'/inc/img/boxes/box2.png';
 
$box_first = 'linear-gradient(rgba(0, 175, 227, .8), rgba(0, 175, 227, .8)),
url("$tpl_dir") center;';
,但没有成功

你对我有什么结论吗


非常感谢并保持健康,丹尼斯我不太确定我是否明白你的意思。无论如何,我会在这里指出一些问题

如果要分配函数返回的值,则不必回显它。这样就可以像这样删除
echo

$tpl_dir = get_template_directory_uri() . 
'/inc/img/boxes/box2.png';
这里,函数返回的值(即url)附加到图像的路径

这里的第二个问题是,当你想使用变量中的值,在一个字符串中用单引号括起来,试着进行连接

例如:

否则,它应该用双引号括起来。然后只解析其中的变量

例如:

或者

此外,如果您要遵循这一点而不是串联,我建议将变量名用大括号括起来

例如:

现在可以先使用
$box\u
变量来设置元素的样式。例如:

<div class="abc_something" style="<?php echo $box_first ; ?>">blah blah blah</div>

提前感谢!我想我犯了一个逻辑错误。首先-(“.$tpl_dir.”)运行良好!当我定义$tpl_dir并将其与完整的域链接到文件时,它工作得非常好。实际上,我用一个用于get_模板的var尝试了它$homeptth=get_template_directory_uri()$tpl_dir1=$homeptth'/inc/img/box/box1.png';它似乎不适用于运算符“$tpl\u dir1=get\u template\u directory\u uri()”/inc/img/box/box1.png';当我在教科书中调用此链接时,链接保持正确。我不知道我的错误在哪里。@Dennis请你编辑你的问题,并附上一张截图和你正在使用的实际代码。如果它显示任何错误,也包括在内。另外,请包括预期结果。]谢谢你给我这个机会,但我不希望你做我的工作。
$box_first = "linear-gradient(rgba(0, 175, 227, .8), rgba(0, 175, 227, .8)),
url(\"$tpl_dir\") center;";
$box_first = "linear-gradient(rgba(0, 175, 227, .8), rgba(0, 175, 227, .8)),
url('$tpl_dir') center;";
$box_first = "linear-gradient(rgba(0, 175, 227, .8), rgba(0, 175, 227, .8)),
    url(\"{$tpl_dir}\") center;";
<div class="abc_something" style="<?php echo $box_first ; ?>">blah blah blah</div>